Advertisement
rfmonk

codecs_zlib.py

Jan 29th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 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. # pyflakes says imported hex but doesn't use?
  8.  
  9. import codecs
  10. from cStringIO import StringIO
  11.  
  12. from codecs_to_hex import to_hex
  13.  
  14. buffer = StringIO()
  15. stream = codecs.getwriter('zlib')(buffer)
  16.  
  17. text = 'abcdefghijklmnopqrstuvwxyz\n' * 50
  18.  
  19. stream.write(text)
  20. stream.flush()
  21.  
  22. print 'Original length  :', len(text)
  23. compressed_data = buffer.getvalue()
  24. print 'ZIP compressed   :', len(compressed_data)
  25.  
  26. buffer = StringIO(compressed_data)
  27. stream = codecs.getreader('zlib')(buffer)
  28.  
  29. first_line = stream.readline()
  30. print'Read first line :', repr(first_line)
  31.  
  32. uncompressed_data = first_line + stream.read()
  33. print 'Uncompressed     :', len(uncompressed_data)
  34. print 'Same             :', text == uncompressed_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement