Round-trip an lzma-compressed text fixture
Also published in our Blogger archive.
LZMA's standard-library module can create an XZ container directly from a byte string. Here, the concrete fixture is the UTF-8 text north followed by south, with a newline after each word. lzma.compress creates bytes in FORMAT_XZ, and lzma.decompress restores the payload from those bytes.
The example selects CHECK_CRC64, the documented default integrity check for the XZ format, explicitly so the format choice is visible in the fixture. It then asserts the XZ signature, byte-for-byte restoration, and the decoded line sequence. The signature assertion only identifies the expected container prefix; it neither validates every archive property nor makes untrusted compressed input safe to process. A round-trip test also cannot establish a bound on decompression memory use for arbitrary inputs.
lzma.decompress defaults to automatic recognition of XZ and legacy .lzma containers, but the explicit compression format makes this test's output contract clear. The module was added in Python 3.3, which is the minimum version for this exact example. Some integrity-check capabilities can depend on the linked liblzma build; CRC64 is the documented XZ default. Details are in the lzma module documentation, the one-shot compression reference, and the container-format reference.
import lzma
payload = "north\nsouth\n".encode("utf-8")
compressed = lzma.compress(
payload,
format=lzma.FORMAT_XZ,
check=lzma.CHECK_CRC64,
)
restored = lzma.decompress(compressed)
assert compressed.startswith(b"\xfd7zXZ\x00")
assert restored == payload
assert restored.decode("utf-8").splitlines() == ["north", "south"]
print("restored=" + ",".join(restored.decode("utf-8").splitlines()))
restored=north,south
AI assistance disclosure: this article was drafted with AI assistance and should be reviewed in the context of its project.