🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The zlib module in Python provides functions for compression and decompression using the zlib library. It supports various compression levels and is useful for efficiently compressing and decompressing data.
Table of Contents
- Introduction
- Key Functions
compressdecompresscompressobjdecompressobjadler32crc32
- Examples
- Basic Compression and Decompression
- Using Compression and Decompression Objects
- Calculating Checksums
- Real-World Use Case
- Conclusion
- References
Introduction
The zlib module provides access to the zlib library, which is used for data compression. It allows for the compression and decompression of data using the DEFLATE compression algorithm, which is commonly used in formats like gzip and PNG.
Key Functions
compress
Compresses the input data to the specified level (between 0 and 9).
import zlib
data = b'Hello, World!'
compressed_data = zlib.compress(data, level=9)
print(compressed_data)
decompress
Decompresses the input data.
import zlib
decompressed_data = zlib.decompress(compressed_data)
print(decompressed_data)
compressobj
Creates a compression object for more advanced compression.
import zlib
compressor = zlib.compressobj(level=9)
compressed_data = compressor.compress(data)
compressed_data += compressor.flush()
print(compressed_data)
decompressobj
Creates a decompression object for more advanced decompression.
import zlib
decompressor = zlib.decompressobj()
decompressed_data = decompressor.decompress(compressed_data)
decompressed_data += decompressor.flush()
print(decompressed_data)
adler32
Computes the Adler-32 checksum of the input data.
import zlib
checksum = zlib.adler32(data)
print(checksum)
crc32
Computes the CRC-32 checksum of the input data.
import zlib
checksum = zlib.crc32(data)
print(checksum)
Examples
Basic Compression and Decompression
import zlib
# Data to be compressed
data = b'This is some data that needs to be compressed.'
# Compress the data
compressed_data = zlib.compress(data)
print(f"Compressed Data: {compressed_data}")
# Decompress the data
decompressed_data = zlib.decompress(compressed_data)
print(f"Decompressed Data: {decompressed_data}")
Using Compression and Decompression Objects
import zlib
# Data to be compressed
data = b'This is some data that needs to be compressed using objects.'
# Create a compressor object
compressor = zlib.compressobj(level=9)
compressed_data = compressor.compress(data)
compressed_data += compressor.flush()
print(f"Compressed Data: {compressed_data}")
# Create a decompressor object
decompressor = zlib.decompressobj()
decompressed_data = decompressor.decompress(compressed_data)
decompressed_data += decompressor.flush()
print(f"Decompressed Data: {decompressed_data}")
Calculating Checksums
import zlib
# Data to be checksummed
data = b'This is some data to calculate checksum.'
# Calculate Adler-32 checksum
adler32_checksum = zlib.adler32(data)
print(f"Adler-32 Checksum: {adler32_checksum}")
# Calculate CRC-32 checksum
crc32_checksum = zlib.crc32(data)
print(f"CRC-32 Checksum: {crc32_checksum}")
Real-World Use Case
Compressing and Decompressing a File
import zlib
# Compress a file
def compress_file(input_file, output_file):
with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out:
compressor = zlib.compressobj(level=9)
while chunk := f_in.read(1024):
f_out.write(compressor.compress(chunk))
f_out.write(compressor.flush())
# Decompress a file
def decompress_file(input_file, output_file):
with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out:
decompressor = zlib.decompressobj()
while chunk := f_in.read(1024):
f_out.write(decompressor.decompress(chunk))
f_out.write(decompressor.flush())
# Example usage
compress_file('example.txt', 'example.txt.zlib')
decompress_file('example.txt.zlib', 'example_decompressed.txt')
Conclusion
The zlib module in Python provides a straightforward way to compress and decompress data using the DEFLATE compression algorithm. It supports both simple and advanced use cases, including checksum calculations and working with compression objects.
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment