Advertisement
rfmonk

zlib_incremental.py

Jan 30th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 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 zlib
  8. import binascii
  9.  
  10. compressor = zlib.compressobj(1)
  11.  
  12. with open('lorem.txt', 'r') as input:
  13.     while True:
  14.         block = input.read(64)
  15.         if not block:
  16.             break
  17.         compressed = compressor.compress(block)
  18.         if compressed:
  19.             print 'Compressed: %s' % binascii.hexlify(compressed)
  20.         else:
  21.             print 'buffering...'
  22.     remaining = compressor.flush()
  23.     print 'Flushed: %s' % binascii.hexlify(remaining)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement