Unless you really know what you're doing I'd recommend to not use it and just use the default value. The strategy argument relates to compression tuning. Currently the only possible value is DEFLATED, which is the only method defined in the RFC 1950. The method argument represents the compression algorithm used. Includes gzip header and trailing checksum The main difference between the arguments of this function and the compress() function is (aside from the data parameter) the wbits argument, which controls the window size, and whether or not the header and trailer are included in the output. The syntax is as follows: compressobj(level=- 1, method=DEFLATED, wbits= 15, memLevel= 8, strategy=Z_DEFAULT_STRATEGY) Large data streams can be managed with the compressobj() function, which returns a compression object. If you don't format the string as hexadecimal, as I've done in this example, and view the output data you'll probably notice that the input string is still readable even after being "compressed", although it has a few extra formatting characters around it. This difference in length is due to the lack of compression in using level 0. Using a level of 2 we get a string (formatted in hexadecimal) of length 38, whereas with a level of 0 we get a hex string with length 44. You may notice a few differences comparing the outputs when using 0 or 2 for the compression level. If we change the level to 0 (no compression), then line 5 becomes: compressed_data = press(data, 0)Īnd the new result is: $ python compress_str.pyĬompressed data: 7801010b00f4ff48656c6c6f20776f726c6418ab043d Print( 'Compressed data: ' + binascii.hexlify(compressed_data))Īnd the result is as follows: $ python compress_str.pyĬompressed data: 785ef348cdc9c95728cf2fca49010018ab043d Level 0 yields no compression.Īn example of using the compress method on a simple string is shown below: The default value has a balance between speed and compression. The value -1 represents the default, which is level 6. Level 9 is the slowest, yet it yields the highest level of compression. This parameter determines the level of compression, where level 1 is the fastest and yields the lowest level of compression. Here the argument data contains the bytes to be compressed, and level is an integer value that can take the values -1 or 0 to 9. The syntax of this function is very simple, taking only two arguments: compress(data, level=- 1) The zlib library provides us with the compress function, which can be used to compress a string of data. One of the best things, in my opinion, about the zlib library is that it is compatible with the gzip file format/tool (which is also based on DEFLATE), which is one of the most widely used compression applications on Unix systems. Both modes of operation are explained in this article. Compression and decompression can both be done as a one-off operations, or by splitting the data into chunks like you'd seem from a stream of data. The most important functionalities included in this library are compression and decompression. The main use of the zlib library is in applications that require compression and decompression of arbitrary data, whether it be a string, structured in-memory content, or files. Another important benefit of this compression mechanism is that it doesn't expand the data. It is a lossless compression format (which means you don't lose any data between compression and decompression), and has the advantage of being portable across different platforms. The zlib compression format is free to use, and is not covered by any patent, so you can safely use it in commercial products as well. The data format used by the library is specified in the RFC 1950 to 1952, which is available at. The Python zlib library provides a Python interface to the zlib C library, which is a higher-level abstraction for the DEFLATE lossless compression algorithm.
0 Comments
Leave a Reply. |