The 'zstd' library provides fast Python bindings to Yann Collet's Zstandard (zstd) lossless compression algorithm. It offers a compelling balance of speed and compression ratio, making it a popular choice for real-time compression and large-scale data processing. The library is actively maintained with frequent releases, currently at version 1.5.7.3, and focuses on performance optimizations and bug fixes.
pip install zstdVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic one-shot compression and decompression of bytes data using `zstd.compress()` and `zstd.decompress()`. It also includes an example of streaming compression/decompression for larger datasets, highlighting the use of `ZstdCompressor` and `ZstdDecompressor` and the importance of flushing.
For Python 3.14+, explicitly use `from backports import zstd` if you intend to use the PyPI `zstd` package, or adapt your code to use the standard library's `from compression import zstd` module. For cross-version compatibility, use conditional imports (`if sys.version_info >= (3, 14): from compression import zstd else: import zstd`).
For large datasets, use the streaming APIs provided by `zstd.ZstdCompressor` and `zstd.ZstdDecompressor` which process data in chunks, keeping memory usage manageable. Ensure files are opened in binary mode ('rb', 'wb') for streaming I/O.Always call `compressor.flush()` after the last `compressor.compress()` call to finalize the frame. Using `ZstdCompressor` as a context manager (e.g., `with cctx.stream_writer(fh) as compressor: ...`) can help ensure flushing is handled automatically upon exiting the block.
Review any code that calls `zstd.ZSTD_min_compression_level()` and adapt it to expect a standard integer value for the compression level. Most users are unlikely to be directly affected unless interacting with this specific low-level function.
Install the Zstandard development packages specific to your operating system. For Debian/Ubuntu: `sudo apt-get update && sudo apt-get install libzstd-dev`. For RedHat/CentOS: `sudo dnf install libzstd-devel` or `sudo yum install libzstd-devel`. For macOS (Homebrew): `brew install zstd`.
Install the package using pip: `pip install zstd`
Ensure the data being passed to the `zstd` decompressor was genuinely compressed using Zstandard and has not been altered or partially read. Verify the source and integrity of the compressed data.
For very large data, use `zstd.ZstdCompressor` and `zstd.ZstdDecompressor` in streaming mode by feeding data in manageable chunks (e.g., using `compress_stream` / `decompress_stream` or `stream_writer` / `stream_reader`).
No dependency data recorded yet.