Compression compatible with gzip in Python (zlib)


The zlib module provides Python’s implementation of Zlib compression library (http://www.zlib.net) which is a part of GNU project.

This article discusses important functions defined in zlib module.

compress()

This function is the primary interface to this module along with decompress() function. This function returns byte object by compressing the data given to it as parameter. The function has another parameter called level which controls the extent of compression. It an integer between 0 to 9. Lowest value 0 stands for no compression and 9 stands for best compression. Higher the level of compression, greater the length of compressed byte object.

decompress()

This function does the opposite of compress() function. It retrieves the uncompressed data. This function can have an optional parameter called wbits which controls the size of history buffer and the nature of header and trailer format.

Following statement compresses a string object with the help of compress() function

>>> import zlib
>>> c = zlib.compress(b'hello python')
>>> c
b'x\x9c\xcbH\xcd\xc9\xc9W(\xa8,\xc9\xc8\xcf\x03\x00\x1e\xf0\x04\xd7'

Whereas the decompress() function retrieves original Python string.

>>> zlib.decompress(c)
b'hello python'

The module consists of definitions of has two corresponding classes of compression and decompression objects.

compressobj() − This constructor returns compression object to be used to compress data streams. Two important method of this object are

compress() − returns compressed byte object.

flush() − Since compression object deals with streams, some compressed data may be remaining in buffer. This method empties the buffer.

Following example sets up compression object and constructs a compressed data object by adding the flushed data to the return value of compress() method.

>>> s1 = b'hello python'
>>> c = zlib.compressobj()
>>> s2 = c.compress(s1)
>>> s2 = s2+c.flush()
>>> s2
b'x\x9c\xcbH\xcd\xc9\xc9W(\xa8,\xc9\xc8\xcf\x03\x00\x1e\xf0\x04\xd7'

decompressobj() − This acts as constructor for decompression object which decompresses large data from streams. This object to provides following methods

decompress() − This method returns uncompressed data

flush() − This method empties the buffer.

Following code retrieves uncompressed data from byte object created in previous example

>>> d = zlib.decompressobj()
>>> x = d.decompress(s2)
>>> x = x+d.flush()
>>> x
b'hello python'

Two constants are defined in zlib module which provide version information of zlib library in use.

>>> zlib.ZLIB_VERSION
'1.2.11'
>>> zlib.ZLIB_RUNTIME_VERSION
'1.2.11'

Updated on: 25-Jun-2020

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements