Brotlipy: Python Bindings to Brotli Compression
brotlipy is a Python library that provides CFFI-based bindings to Google's Brotli lossless compression algorithm. It enables Python applications to efficiently compress and decompress data using Brotli, supporting both one-shot and streaming operations. Version 0.7.0 is the final release of this library, as the project has been archived and its development continues under the `brotlicffi` package.
Warnings
- breaking The `brotlipy` package has been archived and is deprecated. All further development and updates have moved to the `brotlicffi` package. Users are strongly advised to migrate to `brotlicffi` for continued support and new features.
- gotcha The `brotlipy` library installs as `brotlipy` but its main module is imported as `brotli`. This can create import conflicts if another Python package also provides a top-level `brotli` module (e.g., the pure-Python `brotli` package). The successor `brotlicffi` addresses this by using `import brotlicffi`.
- gotcha On Linux systems, `brotlipy` is a CFFI-based binding that requires compilation during installation. This necessitates having a C compiler (e.g., `build-essential` or `gcc`), Python development headers (`python-dev` or `python-devel`), and the `libffi` development library (`libffi-dev` or `libffi-devel`) installed on the system.
- gotcha When using dictionaries with the compression and decompression APIs, it is crucial that the *exact same dictionary* is provided to both the compressor and the decompressor. A mismatch will lead to decompression errors or corrupted output.
Install
-
pip install brotlipy -
sudo apt-get install build-essential python-dev libffi-dev -
sudo yum install gcc libffi-devel python-devel
Imports
- brotli
import brotli
- compress
brotli.compress(data)
- decompress
brotli.decompress(data)
- Compressor
brotli.Compressor()
- Decompressor
brotli.Decompressor()
- Error
brotli.Error
Quickstart
import brotli
original_data = b"This is some data to be compressed using Brotli. It can be any bytes string."
# One-shot compression and decompression
compressed_data = brotli.compress(original_data, quality=11) # quality from 0-11
decompressed_data = brotli.decompress(compressed_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")
print(f"Decompressed data matches original: {original_data == decompressed_data}")
# Streaming compression
compressor = brotli.Compressor(quality=5)
compressed_stream = b''
chunk_size = len(original_data) // 2
compressed_stream += compressor.compress(original_data[:chunk_size])
compressed_stream += compressor.compress(original_data[chunk_size:])
compressed_stream += compressor.finish()
# Streaming decompression
decompressor = brotli.Decompressor()
decompressed_stream = b''
decompressed_stream += decompressor.decompress(compressed_stream[:chunk_size])
decompressed_stream += decompressor.decompress(compressed_stream[chunk_size:])
decompressed_stream += decompressor.flush() # flush() is deprecated for Decompressor since 0.4.0 but shown in older docs
print(f"Streaming decompression matches original: {original_data == decompressed_stream}")