Python zlib Library | what is zlib in Python?The zlib is a Python library that supports the zlib C library, a higher-level generalization for deflating lossless compression algorithms. The zlib library is used to lossless compress, which means there is no data loss between compression and decompression). It also provides portability advantages across the different platforms and doesn't expand the data. This library plays a very significant role in terms of security. Many applications require the compression and decompression of arbitrary data such as strings, files, or structured in-memory content. This library is well-suited with the gzip file format/tool, the most popular and useful compression application on UNIX systems. The compression() methodThe zlib library facilitates us to compress() method, which is used to compress a data string. Below is the syntax of the function. Parameter -
Let's understand the below example. Example - Output: Original data: Welcome to JavaTpoint Compressed data: 785ef348cdc9c95728cf2fca49010018ab043d If we change the value 2 to 0, then the result will be as below. Compressing Large Data StreamsThe zlib library provides the comressobj() function to manage large data streams. This method returns the compression object. The syntax is given below. Syntax - Parameters -
Let's understand the following example Example - Output: Original: Hello world Compressed data: f348cdc9c95728cf2fca490100 Explanation - We took a simple string value that is not a large data stream, it serves the purpose of showing the working of compressobj() function. The string "Welcome to JavaTpoint" has compressed. Generally, this method is used when the data streams are too large or won't fit into memory. This method plays essential role in a bigger application where we can configure the compression and can be used to compress portions of the data in series. It plays the significant role where the compression is required. With the help of compress.compress(data) method, we can compress and flush the chunk of data without accumulating full data in memory. Compressing a FileWe will use the compression() method to compress the file. The syntax is similar as in the previous example. In the below example, we will compress the PNG image "mountain.png". Let's understand the following example. Example - Output: Compressed: 10% In the above example, we have used the Z_BEST_COMPRESSION, which is the best compression level this algorithm has to offer. In the next line, we calculate the level of compression based on the ratio of the compressed data length over the original data. The file is compressed by 13%, this is how to compress the ASCII string or binary image data. Saving Compress Data to a FileWe can also save the compressed data in a file for the further use. In the below example, we shows some compressed text into a file. Example - When we run the above program, it compresses the given string and saves the compressed data into a file named "output.txt". DecompressionDecompressing is also an important aspect of the application. The zlib library provides the decompress() method. Below is the syntax of it. Syntax: Parameters -
Let's understand the following example. Example - Output: Welcome to JavaTpoint Decompressing Large Data StreamsWhile decompressing the large data stream, we may face the memory management issue due to the size or source of the data. There is a chance we may not utilize all of the available memory for that particular task. So that, the decompressobj() allows us to divide up a large stream in multiple chunks, which can be decompressed separately. The syntax is as below. Syntax - The above method returns decompress object, which is used to decompress the particular data. Let's understand the following example. Example - Output: Data Before Decompress: Welcome to JavaTpoint Data After Decompress - #@$%%#@@#s Decompressed Data: Welcome to JavaTpoint Decompressing Data from a FileAs we have discussed in previous examples, we can easily decompress the data contained in the file. This example is similar to the previous example; we get the data from the file, except that in this case, we will use the decompress() method. This method is useful when data is small enough to fit in the memory easily. Let's understand the following example. Example - Explanation - We have read the hello.dat that contains "Welcome to JavaTpoint". However, the file contains the a small string so we have used the decompress() instead of decompressobj() function. ConclusionThe Python zlib library is useful when the application requires compression at a security level. It comes with a bunch of excellent functions. We have discussed some important concepts of the zlib library, although many functions are available. The compress() and decompress() methods are used for the small data, whereas compressobj() and decompressobj() methods are available to provide more flexibility by supporting compression/decompression of data streams. Next TopicPython Queue Module |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India