{"id":1689,"library":"pyzstd","title":"pyzstd: Zstandard Compression","description":"pyzstd provides high-performance Python bindings for the Zstandard (zstd) lossless compression algorithm. It enables fast compression and decompression for bytes-like objects and file streams, leveraging the C zstd library. The current stable version is 0.19.1, and it is actively maintained with regular releases.","status":"active","version":"0.19.1","language":"en","source_language":"en","source_url":"https://github.com/Rogdham/pyzstd","tags":["compression","zstd","data-processing","performance"],"install":[{"cmd":"pip install pyzstd","lang":"bash","label":"Install pyzstd"}],"dependencies":[],"imports":[{"note":"The installed module name is `zstd`, not `pyzstd`.","wrong":"import pyzstd","symbol":"zstd","correct":"import zstd"},{"symbol":"ZstdCompressor","correct":"from zstd import ZstdCompressor"},{"symbol":"ZstdDecompressor","correct":"from zstd import ZstdDecompressor"}],"quickstart":{"code":"import zstd\n\noriginal_data = b\"This is some sample data that will be compressed using Zstandard!\"\n\n# Compress data with default level (3)\ncompressed_data = zstd.compress(original_data)\nprint(f\"Original size: {len(original_data)} bytes\")\nprint(f\"Compressed size: {len(compressed_data)} bytes\")\n\n# Decompress data\ndecompressed_data = zstd.decompress(compressed_data)\n\n# Verify data integrity\nassert original_data == decompressed_data\nprint(\"Data decompressed successfully and matches original.\")\n\n# Example with a specific compression level\ncompressed_level_20 = zstd.compress(original_data, 20)\nprint(f\"Compressed size (level 20): {len(compressed_level_20)} bytes\")","lang":"python","description":"This quickstart demonstrates basic compression and decompression of bytes-like objects using the top-level `zstd.compress` and `zstd.decompress` functions, including specifying a compression level."},"warnings":[{"fix":"For file-like objects, use `zstd.ZstdCompressor().compress_stream()` or `zstd.ZstdDecompressor().decompress_stream()`. Alternatively, use `zstd.open_compressed_file()` for stream-based file I/O.","message":"In version 0.17.0, the `zstd.compress_file` and `zstd.decompress_file` functions were removed, and the signature of `zstd.open` was changed significantly. Direct file handling methods are no longer available at the top level.","severity":"breaking","affected_versions":">=0.17.0"},{"fix":"For efficient processing of file-like objects (e.g., `io.BytesIO`, file handles), instantiate `zstd.ZstdCompressor()` or `zstd.ZstdDecompressor()` and use their `compress_stream()` or `decompress_stream()` methods.","message":"The top-level `zstd.compress()` and `zstd.decompress()` functions are designed for bytes-like objects in memory. Attempting to pass file objects directly to them will result in errors or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully select compression levels based on your application's balance of compression ratio and performance requirements. Always ensure the level is within the valid range [1, 22].","message":"Compression levels in `pyzstd` range from 1 to 22 (inclusive), with 3 being the default. Higher levels offer better compression but require significantly more CPU time. Using a value outside this range will raise an error.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}