Use this file to discover Exa products, pricing, evaluations, comparisons, use cases, agent interfaces, and technical documentation before exploring further.
Python compression libraries shrink data for storage or transfer and restore it byte for byte. Each codec trades compression ratio against speed, from fast codecs for streaming to slow codecs that maximize savings. These libraries let data pipelines, web servers, and file formats cut storage and bandwidth costs.
Popularity
python-zstandard has the most monthly downloads in this comparison, at 188.2M.
def zstd_decode(payload):
if not zstd:
raise NotImplementedError("Zstd codec is not available")
with zstd.ZstdDecompressor().stream_reader(io.BytesIO(payload), read_across_frames=True) as reader:
return reader.read()
def compressor_file(self, fileobj, compresslevel=None):
"""Returns an instance of a compressor file object."""
self._check_versions()
if compresslevel is None:
return self.fileobj_factory(fileobj, "wb")
else:
return self.fileobj_factory(fileobj, "wb", compression_level=compresslevel)
def decompress(self, data: bytes, max_length: int = -1) -> bytes:
if not data and not self.has_unconsumed_tail:
return b""
if self._obj.eof:
data = self._obj.unused_data + data
self._obj = zstd.ZstdDecompressor()
part = self._obj.decompress(data, max_length=max_length)
length = len(part)
data_parts = [part]
# Every loop iteration is supposed to read data from a separate frame. # The loop breaks when: # - enough data is read; # - no more unused data is available; # - end of the last read frame has not been reached (i.e., # more data has to be fed). while (
self._obj.eof
and self._obj.unused_data
and (max_length < 0 or length < max_length)
):
unused_data = self._obj.unused_data
if not self._obj.needs_input:
self._obj = zstd.ZstdDecompressor()
part = self._obj.decompress(
unused_data,
max_length=(max_length - length) if max_length > 0 else -1,
)
if part_length := len(part):
data_parts.append(part)
length += part_length
elif self._obj.needs_input:
break
return b"".join(data_parts)
def compress(data, level=ZLIB_COMPRESSION_LEVEL):
"""Compress 'data' to Zlib format. If 'USE_ZOPFLI' variable is True,
zopfli is used instead of the zlib module.
The compression 'level' must be between 0 and 9. 1 gives best speed,
9 gives best compression (0 gives no compression at all).
The default value is a compromise between speed and compression (6).
"""
if not (0 <= level <= 9):
raise ValueError("Bad compression level: %s" % level)
if not USE_ZOPFLI or level == 0:
from zlib import compress
return compress(data, level)
else:
from zopfli.zlib import compress
return compress(data, numiterations=ZOPFLI_LEVELS[level])
zstandard suits general compression with a strong ratio at high speed. LZ4 suits workloads that need the fastest compression and decompression. Brotli suits web assets served to browsers.
Brotli usually compresses text smaller than gzip at a similar decompression speed. Browsers accept both encodings for HTTP responses.
Zstandard compresses faster than gzip at a similar or higher ratio and decompresses much faster. gzip remains the choice where only the standard library or older tools are available.
The standard library includes zlib, gzip, and lzma modules. Recent releases add a Zstandard module, and backports.zstd brings that module to older versions.
cramjam bundles many compression algorithms, including Snappy, Brotli, and Zstandard, behind one set of Rust-backed bindings.
For coding agents
Found your compression library? Help your agent use it.
Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.