
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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'
- Related Articles
- How To enable GZIP Compression in PHP?
- How to configure nginx with gzip module for compression on centos 7
- Python Support for gzip files (gzip)
- Is C++0x Compatible with C?
- Program to perform string compression in Python
- Which version of Firefox is compatible with selenium
- Python Support for bzip2 compression (bz2)
- Using gzip and gunzip in Linux
- Difference between Lossy Compression and Lossless Compression
- Program to perform prefix compression from two strings in Python
- Compression using the LZMA algorithm using Python (lzma)
- Data compression in SAP HANA
- Using xz Compression in Linux
- What is the most compatible way to install python modules on a Mac?
- Enable MySQL Compression
