BrotliCFFI
BrotliCFFI provides Python CFFI bindings to Google's high-performance Brotli compression library. It allows Python applications to leverage the Brotli compression algorithm efficiently across various Python interpreters, including PyPy. The current version is 1.2.0.1, and it maintains an active release cadence with regular updates to the underlying Brotli library and Python compatibility.
Warnings
- breaking Package renamed from `brotlipy` to `brotlicffi` and import namespace changed from `brotli` to `brotlicffi` to avoid conflicts with Google's official Brotli C bindings.
- breaking The `dictionary` parameter was removed from `brotlicffi.compress()` and `brotlicffi.Compressor`.
- breaking Support for Python 2.7, 3.5, and 3.6 was explicitly removed.
- breaking Concurrent sharing of `Compressor` or `Decompressor` objects across multiple threads (especially with Python 3.14's free-threaded build) now raises an exception.
- gotcha `decompress()` would return an empty bytestring instead of erroring if the provided bytestring was too small or malformed.
- gotcha The `output_buffer_limit` parameter was added to `Decompressor.decompress()` and `Decompressor.process()` methods to mitigate potential security concerns from maliciously crafted compressed data leading to excessive memory usage.
Install
-
pip install brotlicffi -
conda install -c conda-forge brotlicffi
Imports
- brotlicffi
import brotlicffi
Quickstart
import brotlicffi
original_data = b"This is some sample data that will be compressed using Brotli!"
# Compress data
compressed_data = brotlicffi.compress(original_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")
# Decompress data
decompressed_data = brotlicffi.decompress(compressed_data)
print(f"Decompressed data: {decompressed_data}")
assert original_data == decompressed_data
print("Compression and decompression successful!")