The zlib module provides a lower-level interface to many of the functions in the zlib compression library from the GNU project.

Working with Data in Memory


In [2]:
import zlib
import binascii
origin_data = b'This is the original text.'
print('Original   :', len(origin_data), origin_data)
compressed = zlib.compress(origin_data)
print('Compressed  :', len(compressed), binascii.hexlify(compressed))
decompressed = zlib.decompress(compressed)
print('Decompressed:', len(decompressed), decompressed)


Original   : 26 b'This is the original text.'
Compressed  : 32 b'789c0bc9c82c5600a2928c5485fca2ccf4ccbcc41c8592d48a123d007f2f097e'
Decompressed: 26 b'This is the original text.'

In [3]:
import zlib

original_data = b'This is the original text.'

template = '{:>15}  {:>15}'
print(template.format('len(data)', 'len(compressed)'))
print(template.format('-' * 15, '-' * 15))

for i in range(5):
    data = original_data * i
    compressed = zlib.compress(data)
    highlight = '*' if len(data) < len(compressed) else ''
    print(template.format(len(data), len(compressed)), highlight)


      len(data)  len(compressed)
---------------  ---------------
              0                8 *
             26               32 *
             52               35 
             78               35 
            104               36 

In [4]:
import zlib

input_data = b'Some repeated text.\n' * 1024
template = '{:>5}  {:>5}'

print(template.format('Level', 'Size'))
print(template.format('-----', '----'))

for i in range(0, 10):
    data = zlib.compress(input_data, i)
    print(template.format(i, len(data)))


Level   Size
-----   ----
    0  20491
    1    172
    2    172
    3    172
    4     98
    5     98
    6     98
    7     98
    8     98
    9     98