Python zlib.compress(data, /, level=-1)

0
zlib.compress()

The zlib.compress() method is used to compress the bytes presents in data. It returns an object of compressed data. It is used to compress the data passed to it. We can control the level of the compression.

zlib.compress(data)

By passing only one parameter, it will compress the data ( should be a byte type object) with the default value of compression. The default compression level is -1, so this method will take no longer to run.

Example No 1. zlib.compress(data)


import zlib
compressed_data= zlib.compress(b'alixaprodev.com')
print(compressed_data)

zlib.compress(data,1)

We can set the level value from 0 to 9. we can control the level of compression with the help of the level value. setting the level value to 1 means the variable (Z_BEST_SPEED) will make the program faster and will produce a value that will be less compressed. while on the other hand, if set the value to 9, it will take longer but will produce a high compressed result.

Example No 2. zlib.compress(data,level)


import zlib
compressed_data= zlib.compress(b'alixaprodev.com',9)
print(compressed_data)

zlib.compressobj()

In order to compress the data that are coming from a stream, the good idea is to make chunks of it and then compressed them separately as compressing a big chunk will take more memory. so we can specify how many chunks and it how many levels we want the compression to be done.

zlib. compress() returns an Object that can be used for compressing the data streams that can not be fit in memory, all at once.

Example No.3 zlib.compressobj(level)


import zlib
# level of compression is 3
compressed_obj= zlib.compressobj(level=3)
print(compressed_obj.compress(b'alixaprodev.com'))

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !