Python Glossary Русский

Python 3.3+lzmafunction

lzma.compress()

in-memory compression

Compresses bytes with LZMA in a single call; by default the result is an .xz container with a CRC64 checksum.

Syntax

data = lzma.compress(data, format=FORMAT_XZ, check=-1, preset=None, filters=None)

LZMA usually compresses tighter than bzip2 and zlib, but it is the slowest of the three to compress, while decompression stays fast. Encode a string to bytes first. The compressed bytes depend on the liblzma version and the parameters, so tests compare a round trip, not the output. format=FORMAT_ALONE is only for compatibility with legacy .lzma.

Examples

import lzma
raw = b'abcdef' * 1000
packed = lzma.compress(raw, preset=6)
print(len(packed) < len(raw), lzma.decompress(packed) == raw)  # → True True
print(packed[:6])  # → b'\xfd7zXZ\x00' — сигнатура контейнера xz

See also

Open in the full glossarydocs.python.orgReport an inaccuracy