cramjam
cramjam provides extremely thin and easy-to-install Python bindings to various de/compression algorithms implemented in Rust. It offers access to popular algorithms like Snappy, Brotli, Bzip2, Lz4, Gzip, Zlib, Deflate, ZSTD, and XZ/LZMA. The library, currently at version 2.11.0, is actively maintained with a regular release cadence.
Warnings
- gotcha When integrating `cramjam`'s LZ4 implementation or migrating from other LZ4 libraries, be aware that the default compression level in `cramjam`'s LZ4 might differ. For example, a change from level 0 to 9 in a dependent library using `cramjam`'s LZ4 was observed to cause significant performance degradation (over 100% latency increase) if not explicitly set. Always verify the `compression_level` if performance is critical.
- gotcha For significant performance improvements (1.5-3x speedup), especially when decompressing or compressing to a standard `bytes` or `bytearray` object, provide the `output_len` argument if the exact output length is known beforehand. This allows for single buffer allocation. This optimization is less relevant when using `cramjam.Buffer` or `cramjam.File` objects.
- gotcha Some compression algorithms (e.g., Blosc2, ISA-L backends like igzip, ideflate, izlib) are considered experimental. These typically require building `cramjam` from source with specific feature flags enabled. They are not available out-of-the-box with a standard `pip install`.
Install
-
pip install --upgrade cramjam
Imports
- cramjam
import cramjam
- snappy
from cramjam import snappy
- Buffer
from cramjam import Buffer
Quickstart
import cramjam
original_data = b"This is some data to compress using cramjam!"
# Compress data using Snappy
compressed_data = cramjam.snappy.compress(original_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size (Snappy): {len(compressed_data)} bytes")
# Decompress data
decompressed_data = cramjam.snappy.decompress(compressed_data)
print(f"Decompressed data matches original: {original_data == decompressed_data}")
# Example with Brotli
compressed_brotli = cramjam.brotli.compress(original_data)
print(f"Compressed size (Brotli): {len(compressed_brotli)} bytes")
decompressed_brotli = cramjam.brotli.decompress(compressed_brotli)
print(f"Decompressed Brotli matches original: {original_data == decompressed_brotli}")
# Using cramjam.Buffer for in-place operations
from cramjam import snappy, Buffer
import numpy as np
data_np = np.frombuffer(b'some bytes here for buffer', dtype=np.uint8)
output_buffer = Buffer()
snappy.compress_into(data_np, output_buffer)
output_buffer.seek(0) # Reset buffer position for reading
decompressed_buffer_data = snappy.decompress(output_buffer)
print(f"Buffer decompressed data matches original: {bytes(data_np) == bytes(decompressed_buffer_data)}")