Python 3.14+compression.zstdterm
compression.zstd.ZstdCompressor
incremental compression
An incremental Zstandard compressor: feed data in parts with compress() and finish the frame with flush().
Syntax
comp = compression.zstd.ZstdCompressor(level=None, options=None, zstd_dict=None); comp.compress(data, mode=ZstdCompressor.CONTINUE); comp.flush()
compress() often returns b'': data accumulates in an internal buffer, and without flush() you get an incomplete frame that decompress rejects. Mode ZstdCompressor.FLUSH_FRAME ends the frame at once, and the same object goes on with the next one. A streaming compressor does not know the input size, so the header lacks it; call set_pledged_input_size(n) before the first compress() if you need it.
Examples
# Python 3.14+
from compression import zstd # → ? нужна сборка CPython с libzstd
comp = zstd.ZstdCompressor()
print(comp.compress(b'abc')) # → b'' — пока всё в буфере
out = comp.compress(b'def') + comp.flush()
print(zstd.decompress(out)) # → b'abcdef'
# Python 3.14+
from compression import zstd # → ? нужна сборка CPython с libzstd
data = b'hello zstd ' * 100
comp = zstd.ZstdCompressor()
comp.set_pledged_input_size(len(data))
out = comp.compress(data) + comp.flush()
print(zstd.get_frame_info(out).decompressed_size) # → 1100
See also
Open in the full glossarydocs.python.orgReport an inaccuracy