Batu Lab NotesPractical developer guides

Round-trip a gzip-compressed text fixture

By Batu ยท English technical notes

Also published in our Blogger archive.

A compact gzip fixture is useful when a test needs realistic compressed input without creating a file. This example starts with two UTF-8 text lines, encodes them to bytes, and passes those bytes to gzip.compress. The result is a gzip-format byte sequence held only in memory. gzip.decompress restores the original bytes, which are decoded and checked as two lines.

The explicit mtime=0 keeps the generated fixture independent of the current clock. That is useful for reproducible test data, but the important assertion here is the round trip: decompression yields exactly the original byte sequence. The gzip magic-byte assertion confirms that the result begins like a gzip stream; it is not a complete validity or security check. Likewise, a successful round trip for this small controlled input does not demonstrate behavior for corrupt, oversized, or adversarial compressed data.

gzip.compress returns bytes and gzip.decompress accepts compressed bytes; the official documentation also notes that decompression can handle concatenated gzip members. The mtime parameter for gzip.compress was added in Python 3.8, so this exact example requires Python 3.8 or later. See the gzip documentation and gzip decompression documentation.

import gzip

payload = "alpha\nbeta\n".encode("utf-8")
compressed = gzip.compress(payload, mtime=0)
restored = gzip.decompress(compressed)

assert compressed.startswith(b"\x1f\x8b")
assert restored == payload
assert restored.decode("utf-8").splitlines() == ["alpha", "beta"]

print("restored=" + restored.decode("utf-8").replace("\n", "|").rstrip("|"))
restored=alpha|beta

AI assistance disclosure: this article was drafted with AI assistance and should be reviewed in the context of its project.