How to Compress a String to Gzip Bytes in Python
Compress a string into gzip-compressed bytes entirely in memory using the standard library gzip module.
Python code
12 linesimport gzip
def compress_to_gzip_bytes(data: str, encoding: str = "utf-8") -> bytes:
"""Compress a string to gzip-compressed bytes in memory."""
return gzip.compress(data.encode(encoding))
if __name__ == "__main__":
original = "Hello, world! " * 10
compressed = compress_to_gzip_bytes(original)
print(f"Original size: {len(original.encode())} bytes")
print(f"Compressed size: {len(compressed)} bytes")
print(f"Compressed bytes (first 20): {compressed[:20]}")
Output
Original size: 130 bytes
Compressed size: 47 bytes
Compressed bytes (first 20): b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed\xc1\x01\x01\x00\x00\x00\x80\x90'
How it works
The gzip.compress function takes a bytes-like object and returns gzip-compressed bytes, avoiding the overhead of writing to a file. Encoding the string with data.encode(encoding) converts it to bytes before compression. This is ideal for in-memory operations like sending compressed payloads over HTTP or storing in caches. The compression level defaults to 9, but you can adjust it with the compresslevel parameter for speed vs. size trade-offs.
Common mistakes
- Forgetting to encode the string to bytes before calling gzip.compress.
- Assuming gzip.compress works directly with str objects.
- Ignoring the compresslevel parameter when speed matters more than size.
- Confusing gzip.compress (bytes in, bytes out) with gzip.open (file operations).
Variations
- Use gzip.compress(data.encode(), compresslevel=6) to balance speed and compression ratio.
- Use zlib.compress for raw deflate without the gzip header if you need smaller size.
Real-world use cases
- Sending compressed JSON payloads to a message queue to reduce network bandwidth.
- Storing compressed logs or text blobs in Redis or a cache to save memory.
- Compressing API responses before writing to a cloud storage object.
Sponsored
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.