zlib-ng Python Bindings
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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
- gotcha The `gzip_ng_threaded.open()` function is designed exclusively for streaming operations. Seeking within files opened with `gzip_ng_threaded.open()` is not supported.
- breaking Version 1.0.0 of `zlib-ng` has dropped official support for Python 3.8 and 3.9.
Install
-
pip install zlib-ng
Imports
- zlib_ng
from zlib_ng import zlib_ng
- gzip_ng
from zlib_ng import gzip_ng
- gzip_ng_threaded
from zlib_ng import gzip_ng_threaded
- zlib
import zlib_ng as zlib
- gzip
import gzip_ng as gzip
Quickstart
from zlib_ng import zlib_ng
original_data = b'This is some data that will be compressed using zlib-ng.'
# Compress data
compressed_data = zlib_ng.compress(original_data)
print(f"Original size: {len(original_data)}")
print(f"Compressed size: {len(compressed_data)}")
# Decompress data
decompressed_data = zlib_ng.decompress(compressed_data)
print(f"Decompressed size: {len(decompressed_data)}")
assert original_data == decompressed_data
print("Data compressed and decompressed successfully!")