{"id":6487,"library":"zlib-ng","title":"zlib-ng Python Bindings","description":"zlib-ng is a Python library that provides high-performance bindings for the zlib-ng compression library. It serves as a drop-in replacement for Python's standard `zlib` and `gzip` modules, offering significantly faster compression and decompression by utilizing optimized algorithms and CPU intrinsics. The library offers `zlib_ng`, `gzip_ng`, and `gzip_ng_threaded` modules. The current version, 1.0.0, emphasizes stability and performance. It generally aligns its development with the upstream zlib-ng C library, with updates primarily focusing on Python compatibility and performance enhancements.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/pycompression/python-zlib-ng","tags":["compression","zlib","gzip","performance","bindings"],"install":[{"cmd":"pip install zlib-ng","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"zlib_ng","correct":"from zlib_ng import zlib_ng"},{"symbol":"gzip_ng","correct":"from zlib_ng import gzip_ng"},{"note":"For multi-threaded streaming compression/decompression.","symbol":"gzip_ng_threaded","correct":"from zlib_ng import gzip_ng_threaded"},{"note":"For drop-in replacement of the standard library's 'zlib' module, it's common to import 'zlib_ng' and alias it as 'zlib'.","wrong":"import zlib_ng","symbol":"zlib","correct":"import zlib_ng as zlib"},{"note":"For drop-in replacement of the standard library's 'gzip' module, it's common to import 'gzip_ng' and alias it as 'gzip'.","wrong":"import gzip_ng","symbol":"gzip","correct":"import gzip_ng as gzip"}],"quickstart":{"code":"from zlib_ng import zlib_ng\n\noriginal_data = b'This is some data that will be compressed using zlib-ng.'\n\n# Compress data\ncompressed_data = zlib_ng.compress(original_data)\nprint(f\"Original size: {len(original_data)}\")\nprint(f\"Compressed size: {len(compressed_data)}\")\n\n# Decompress data\ndecompressed_data = zlib_ng.decompress(compressed_data)\nprint(f\"Decompressed size: {len(decompressed_data)}\")\n\nassert original_data == decompressed_data\nprint(\"Data compressed and decompressed successfully!\")","lang":"python","description":"This quickstart demonstrates basic compression and decompression using the `zlib_ng` module, which mimics the standard `zlib` module's API. It compresses a byte string and then decompresses it, verifying that the original data is recovered."},"warnings":[{"fix":"Avoid using compression level 1 if optimal compression ratio is critical. Use other levels (e.g., default -1, or 2-9) for improved performance and usually better compression.","message":"Compression level 1 in `zlib_ng` offers worse compression rates compared to the standard `zlib` module. For most other compression levels (0 or 2-9), `zlib_ng` provides better compression.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If performing type checks, check for `gzip_ng.GzipNGFile` or rely on duck-typing due to API compatibility. Using `import gzip_ng as gzip` helps maintain consistent code behavior.","message":"The `gzip_ng.open()` function returns a `gzip_ng.GzipNGFile` object, not `gzip.GzipFile` as in the standard library. While `gzip_ng.GzipFile` exists as an alias for compatibility, be aware of the underlying type difference if performing type checks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that your deployment environment has the necessary build tools if pre-built wheels are not available for your specific platform/architecture.","message":"Pre-built wheels for `zlib-ng` are available for common platforms (Linux, Windows, MacOS). For less common or specific system architectures, installation might require building the C `zlib-ng` library from source, which necessitates appropriate build tools (like a C compiler) on the target system.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For files requiring random access or seeking, use `gzip_ng.open()` instead of `gzip_ng_threaded.open()`.","message":"The `gzip_ng_threaded.open()` function is designed exclusively for streaming operations. Seeking within files opened with `gzip_ng_threaded.open()` is not supported.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your project uses Python 3.10 or newer to be compatible with `zlib-ng` version 1.0.0. The `requires_python` metadata is `>=3.9` on PyPI, but documentation clarifies 3.8/3.9 are no longer supported as of 1.0.0.","message":"Version 1.0.0 of `zlib-ng` has dropped official support for Python 3.8 and 3.9.","severity":"breaking","affected_versions":"1.0.0 and later"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}