Advertisement
rfmonk

gzip_compresslevel.py

Jan 30th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import gzip
  8. import os
  9. import hashlib
  10.  
  11.  
  12. def get_hash(data):
  13.     return hashlib.md5(data).hexdigest()
  14.  
  15. data = open('lorem.txt', 'r').read() * 1024
  16. cksum = get_hash(data)
  17.  
  18. print 'Level    Size        Checksum'
  19. print '------   -------  ---------------------'
  20. print 'data     %10d %s' % (len(data), cksum)
  21.  
  22. for i in xrange(1, 10):
  23.     filename = 'compress-level-%s.gz' % i
  24.     with gzip.open(filename, 'wb', compresslevel=i) as output:
  25.         output.write(data)
  26.     size = os.stat(filename).st_size
  27.     cksum = get_hash(open(filename, 'rb').read())
  28.     print '%5d %10d %s' % (i, size, cksum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement