{"id":7884,"library":"zipfile-zstd","title":"zipfile-zstd","description":"zipfile-zstd is a Python library that monkey patches the standard `zipfile` module to enable support for Zstandard compression. It allows both compressing and decompressing files within ZIP archives using the highly efficient Zstandard algorithm. The library is currently at version 0.0.4, with its last release in December 2021, and its primary purpose is to add Zstandard capabilities to Python versions older than 3.14. It relies on the `python-zstandard` library for the underlying Zstandard bindings.","status":"maintenance","version":"0.0.4","language":"en","source_language":"en","source_url":"https://github.com/taisei-project/python-zipfile-zstd","tags":["zip","zstandard","compression","archive","monkey-patch","file-io"],"install":[{"cmd":"pip install zipfile-zstd python-zstandard","lang":"bash","label":"Install with Zstandard dependency"}],"dependencies":[{"reason":"Provides Python bindings for the Zstandard compression library, which is required by zipfile-zstd for Zstandard support.","package":"python-zstandard"}],"imports":[{"note":"Importing this module performs the monkey-patch to enable Zstandard support in the standard `zipfile` module. It also provides `ZIP_ZSTANDARD` constant.","symbol":"zipfile_zstd","correct":"import zipfile_zstd"},{"note":"The `ZIP_ZSTANDARD` constant is added by `zipfile-zstd` to the `zipfile` module after patching, or can be imported directly from `zipfile_zstd` for clarity. Attempting to import it directly from the standard `zipfile` without the patch will fail on Python versions < 3.14.","wrong":"from zipfile import ZIP_ZSTANDARD","symbol":"ZIP_ZSTANDARD","correct":"from zipfile_zstd import ZIP_ZSTANDARD"}],"quickstart":{"code":"import zipfile_zstd\nimport os\nfrom pathlib import Path\n\n# Enable Zstandard support for the standard zipfile module\nzipfile_zstd.enable_zstandard()\n\n# ZIP_ZSTANDARD constant is now available in the patched zipfile module\n# or can be imported directly from zipfile_zstd\nfrom zipfile_zstd import ZIP_ZSTANDARD\nimport zipfile # The standard zipfile module is now patched\n\narchive_name = \"example_zstd.zip\"\nfile_to_compress = \"test_file.txt\"\nextracted_dir = \"extracted_zstd\"\n\n# Create a dummy file for compression\nPath(file_to_compress).write_text(\"This is a test file to be compressed with Zstandard.\\n\")\n\ntry:\n    # 1. Create a Zstandard compressed ZIP archive\n    print(f\"Creating {archive_name} with Zstandard compression...\")\n    with zipfile.ZipFile(archive_name, 'w', compression=ZIP_ZSTANDARD) as zf:\n        zf.write(file_to_compress)\n    print(f\"Successfully created {archive_name}.\")\n\n    # 2. Read and extract from the Zstandard compressed ZIP archive\n    print(f\"Extracting from {archive_name}...\")\n    os.makedirs(extracted_dir, exist_ok=True)\n    with zipfile.ZipFile(archive_name, 'r') as zf:\n        zf.extractall(extracted_dir)\n    print(f\"Content extracted to {extracted_dir}.\")\n\n    # Verify content\n    extracted_file_path = Path(extracted_dir) / file_to_compress\n    if extracted_file_path.exists():\n        print(f\"Extracted content: {extracted_file_path.read_text().strip()}\")\n    else:\n        print(\"Extraction failed or file not found.\")\n\nfinally:\n    # Clean up created files and directories\n    if Path(file_to_compress).exists():\n        os.remove(file_to_compress)\n    if Path(archive_name).exists():\n        os.remove(archive_name)\n    if Path(extracted_dir).exists():\n        import shutil\n        shutil.rmtree(extracted_dir)\n    print(\"Cleanup complete.\")","lang":"python","description":"This quickstart demonstrates how to create a ZIP archive using Zstandard compression and then extract its contents. It first enables the `zipfile-zstd` monkey-patch, then uses the standard `zipfile.ZipFile` class with the `ZIP_ZSTANDARD` constant for compression. It concludes with cleanup of the created files."},"warnings":[{"fix":"For Python 3.14+, prefer the native `zipfile.ZIP_ZSTANDARD` and `compression.zstd` module. For older versions, continue using `zipfile-zstd`.","message":"Python 3.14 and newer versions include native Zstandard support in the standard `zipfile` module (via `compression.zstd` and the `zipfile.ZIP_ZSTANDARD` constant). This `zipfile-zstd` package is intended for compatibility with older Python versions (<3.14). Using it on Python 3.14+ might lead to conflicts or be unnecessary.","severity":"breaking","affected_versions":">=3.14 (CPython)"},{"fix":"For basic Zstandard compression, `zipfile-zstd` is sufficient. For advanced features (like dictionaries or finer control over parameters), use `python-zstandard` directly for compression and then integrate it manually if creating a custom ZIP structure, or consider if Python's native 3.14+ `zipfile` module offers more options.","message":"The `zipfile-zstd` library does not support advanced Zstandard compression parameters or dictionaries, which are available in the underlying `python-zstandard` library. It offers only basic Zstandard compression levels.","severity":"gotcha","affected_versions":"<0.0.4"},{"fix":"Verify compatibility with target systems' ZIP utilities if cross-platform or older tool compatibility is critical. Consider using more widely supported compression methods like Deflate if universal compatibility is a higher priority.","message":"ZIP archives compressed with Zstandard (method ID 93) may not be universally compatible with all ZIP tools, especially older ones (e.g., some versions of Windows Explorer or 7-Zip might require specific forks or updates to handle Zstandard compressed ZIPs).","severity":"gotcha","affected_versions":"All versions"},{"fix":"If encountering out-of-memory errors, reduce the Zstandard compression level. Levels typically between 3-9 offer a good balance of compression ratio and speed for most use cases without excessive memory usage.","message":"Attempting very high Zstandard compression levels (e.g., 20 or higher) within a ZIP context can lead to 'Out of memory' errors, even for small files, depending on the underlying Zstandard implementation and available system resources.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `import zipfile_zstd` (or `zipfile_zstd.enable_zstandard()`) is called before attempting to use `zipfile.ZIP_ZSTANDARD`. Also, verify that `python-zstandard` is installed via `pip install python-zstandard`.","cause":"The `zipfile-zstd` monkey-patch was not applied, or the `python-zstandard` dependency is missing, preventing the patch from introducing `ZIP_ZSTANDARD` into the `zipfile` module. This is common when using older Python versions.","error":"AttributeError: module 'zipfile' has no attribute 'ZIP_ZSTANDARD'"},{"fix":"Install the `python-zstandard` package: `pip install python-zstandard`.","cause":"The `zipfile-zstd` library depends on `python-zstandard` for its Zstandard bindings, which was not installed.","error":"ModuleNotFoundError: No module named 'zstandard'"},{"fix":"Ensure `import zipfile_zstd` (or `zipfile_zstd.enable_zstandard()`) is executed early in your application before any `zipfile.ZipFile` operations involving Zstandard. Verify `python-zstandard` is installed.","cause":"An attempt was made to open or create a Zstandard-compressed ZIP file without the `zipfile` module being correctly patched by `zipfile-zstd`. This means the necessary Zstandard handler is not registered.","error":"RuntimeError: Compression method 93 is not supported"}]}