LZFSE Python Bindings

0.4.2 · active · verified Sat Apr 11

The `lzfse` library provides Python bindings to Apple's LZFSE (Lempel–Ziv Finite State Entropy) compression algorithm. LZFSE is an open-source lossless data compression algorithm developed by Apple, designed for faster decompression and higher energy efficiency compared to zlib, particularly optimized for modern micro-architectures like arm64. The Python bindings are currently at version 0.4.2 (as of April 19, 2024), with infrequent releases often tied to updates in the underlying C reference implementation or improvements to the Python packaging.

Warnings

Install

Imports

Quickstart

This example demonstrates basic LZFSE compression and decompression of a byte string, including error handling for corrupted data.

import lzfse

original_data = b"This is some data to be compressed using LZFSE! " * 100

# Compress data
compressed_data = lzfse.compress(original_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")

# Decompress data
decompressed_data = lzfse.decompress(compressed_data)
print(f"Decompressed size: {len(decompressed_data)} bytes")

# Verify data integrity
assert original_data == decompressed_data
print("Data compressed and decompressed successfully!")

# Handle decompression errors
corrupted_data = b'\xde\xad\xbe\xef' + compressed_data[4:] # Corrupt header
try:
    lzfse.decompress(corrupted_data)
except lzfse.error as e:
    print(f"Caught expected error during decompression: {e}")

view raw JSON →