The zlib module in Python provides functions for data compression and decompression using the DEFLATE algorithm. Below are examples demonstrating how to use zlib to compress and decompress data.
1. Basic Compression and Decompression
This is a simple example showing how to compress and decompress data using zlib.
import zlib# Sample datadata =b"This is some data to compress."# Compress datacompressed_data = zlib.compress(data)print(f"Compressed data: {compressed_data}")# Decompress datadecompressed_data = zlib.decompress(compressed_data)print(f"Decompressed data: {decompressed_data}")
Explanation:
zlib.compress() compresses the input data (in bytes).
zlib.decompress() decompresses the compressed data back to the original form.
2. Compress with Specific Compression Level
You can specify the compression level (from 0 to 9) to control the size and speed of compression.
Explanation:
The level parameter controls the compression level, where 0 is no compression and 9 is maximum compression.
3. Compressing Files
You can also use zlib to compress files. Here’s an example of compressing and decompressing a file.
Explanation:
Reads the file in binary mode ('rb'), compresses it, and writes the compressed data to a new file.
Then reads the compressed file, decompresses it, and writes the decompressed content to a new file.
4. Working with Compression Objects
You can create a zlib compression object that allows you to compress data in chunks.
Explanation:
compressobj() creates a compression object that can be used to compress data in chunks.
The flush() method finalizes the compression process.
A decompressobj() can be used to decompress the data in chunks as well.
5. Compressing Data Using a File-like Object
You can use the zlib module with file-like objects to compress or decompress data.
Explanation:
io.BytesIO() is used to create an in-memory file-like object for reading and writing data.
You can compress and decompress directly with the file-like object.
6. Working with CRC32 Checksum
You can use zlib.crc32() to compute the CRC32 checksum of a data stream.
Explanation:
zlib.crc32() returns the CRC32 checksum of the data, which is often used to verify data integrity.
7. Decompressing with Header Information
You can also decompress files that include header information (like .gz files).
Explanation:
This snippet demonstrates how to handle gzip-compressed data (with headers) using zlib.decompress().
8. Compressing Large Data in Chunks
If you have large data, you can compress it in chunks instead of reading it all into memory at once.
Explanation:
Reads and compresses the file in chunks, which is useful for dealing with large files that don’t fit in memory.
9. Decompressing from a Network Stream
You can decompress data received from a network stream (e.g., from a web server).
Explanation:
This example shows how to handle compressed data that could come from a network stream or file-like object.
10. Compressing a String Using zlib with gzip Format
Compressing data using the gzip format, which includes a header.
Explanation:
This shows how to use the gzip format in zlib for compression and decompression by setting the wbits parameter.
These are some common ways to use zlib for data compression and decompression in Python. You can fine-tune the compression level and work with file-like objects to efficiently handle larger data.
import zlib
data = b"Compress this data with different levels."
# Compress with level 9 (maximum compression)
compressed_data = zlib.compress(data, level=9)
print(f"Compressed data (level 9): {compressed_data}")
# Decompress the data
decompressed_data = zlib.decompress(compressed_data)
print(f"Decompressed data: {decompressed_data}")
import zlib
# Compress a file
with open('example.txt', 'rb') as f:
file_data = f.read()
compressed_data = zlib.compress(file_data)
with open('example.txt.zlib', 'wb') as f:
f.write(compressed_data)
# Decompress the file
with open('example.txt.zlib', 'rb') as f:
compressed_data = f.read()
decompressed_data = zlib.decompress(compressed_data)
with open('decompressed_example.txt', 'wb') as f:
f.write(decompressed_data)
import zlib
compressor = zlib.compressobj()
# Compress data in chunks
compressed_chunk1 = compressor.compress(b"Hello ")
compressed_chunk2 = compressor.compress(b"World!")
compressed_data = compressed_chunk1 + compressed_chunk2 + compressor.flush()
print(f"Compressed data: {compressed_data}")
# Decompress the data
decompressor = zlib.decompressobj()
decompressed_data = decompressor.decompress(compressed_data)
print(f"Decompressed data: {decompressed_data}")
import zlib
import io
data = b"Compress this data using a file-like object."
# Create a file-like object
compressed_stream = io.BytesIO()
# Compress and write to the file-like object
with zlib.compressobj() as compressor:
compressed_stream.write(compressor.compress(data))
compressed_stream.write(compressor.flush())
# Read compressed data from the file-like object
compressed_data = compressed_stream.getvalue()
print(f"Compressed data: {compressed_data}")
# Decompress from the file-like object
decompressed_stream = io.BytesIO(compressed_data)
decompressor = zlib.decompressobj()
decompressed_data = decompressor.decompress(decompressed_stream.read())
print(f"Decompressed data: {decompressed_data}")
import zlib
data = b"Check the checksum of this data."
# Calculate CRC32 checksum
checksum = zlib.crc32(data)
print(f"CRC32 checksum: {checksum}")
import zlib
# Compressed data with header (e.g., a gzipped file)
compressed_data = b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\x4c\x4a\x2c\x28\x29\x4e\x55\x60\x00\x00\x00'
# Decompress the gzipped data
decompressed_data = zlib.decompress(compressed_data, zlib.MAX_WBITS | 16) # 16 for gzip format
print(f"Decompressed data: {decompressed_data}")
import zlib
def compress_large_file(input_file, output_file):
compressor = zlib.compressobj()
with open(input_file, 'rb') as f, open(output_file, 'wb') as out:
while chunk := f.read(1024): # Reading in 1 KB chunks
out.write(compressor.compress(chunk))
out.write(compressor.flush())
compress_large_file('large_file.txt', 'large_file.txt.zlib')
import zlib
import io
# Simulating receiving compressed data from a network
compressed_data = zlib.compress(b"Data from network stream")
# Decompress using BytesIO as a stream
decompressor = zlib.decompressobj()
decompressed_data = decompressor.decompress(compressed_data)
print(f"Decompressed data: {decompressed_data}")
import zlib
data = b"String data to compress using gzip format."
# Compress using gzip format (with header)
compressed_data = zlib.compress(data, level=9, wbits=zlib.MAX_WBITS | 16)
# Decompress the gzip data
decompressed_data = zlib.decompress(compressed_data, wbits=zlib.MAX_WBITS | 16)
print(f"Decompressed data: {decompressed_data}")