LZ4 Bindings for Python

4.4.5 · active · verified Sat Mar 28

The lz4 library provides Python bindings for the high-performance LZ4 compression algorithm by Yann Collet. It supports both the frame and block formats, with the frame format being recommended for most applications due to its interoperability. The library is actively maintained with frequent releases, currently at version 4.4.5, and offers a Pythonic API that can serve as a drop-in alternative to standard library compression modules like `zlib` or `gzip`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic compression and decompression using the recommended `lz4.frame` module. It generates random bytes, compresses them into an LZ4 frame, then decompresses the data and verifies its integrity. Note that input data must be in bytes format.

import lz4.frame
import os

original_data = os.urandom(1024 * 1024) # 1 MB of random bytes

# Compress data using the LZ4 frame format
compressed_data = lz4.frame.compress(original_data)

# Decompress data
decompressed_data = lz4.frame.decompress(compressed_data)

# Verify integrity
assert original_data == decompressed_data
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")
print("Data compressed and decompressed successfully.")

view raw JSON →