compress-utils 0.10.1

Unified interface to zstd, brotli, zlib, gzip, bzip2, lz4, xz/lzma and snappy compression, over a single C core.
Documentation

compress-utils (Rust)

Rust build License: MIT

Unified, safe Rust interface for eight compression algorithms — Zstandard, Brotli, zlib, gzip, bzip2, LZ4, XZ/LZMA, and Snappy — over a single high-performance C core. Same API for every algorithm.

Installation

[dependencies]
compress-utils = { git = "https://github.com/dupontcyborg/compress-utils" }

The crate compiles the bundled C sources from source with the cc crate, with no prebuilt library and no network beyond the crate fetch, so it works offline and on docs.rs. You need only a C/C++ compiler (gcc, clang or MSVC). The first build compiles the codecs and is then cached by Cargo.

The crate is pure C: every codec, including Snappy (the C andikleen/snappy-c port), compiles to C, so there is no libc++ or libstdc++ runtime dependency. Windows is supported by the C core, and the crate's source build works with MSVC.

Quick start

use compress_utils::{compress, decompress, Algorithm};

fn main() -> Result<(), compress_utils::Error> {
    let data = b"the quick brown fox ...";
    let packed   = compress(Algorithm::Zstd, data, 5)?;   // level 1 (fast)..10 (small)
    let restored = decompress(Algorithm::Zstd, &packed)?;
    assert_eq!(&restored, data);
    Ok(())
}

decompress recovers the size from the wire format when the codec records it (zstd, xz, lz4, snappy) and transparently falls back to streaming otherwise, so you don't need to know which is which. It is bounded by set_max_decompressed_size (default 1 GiB) to stay safe against malformed "decompression bomb" inputs.

Streaming (std::io::Read / Write)

use std::io::{Read, Write};
use compress_utils::{Algorithm, Compressor, Decompressor};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Compress into any Write. finish() flushes and finalizes the frame, so call
    // it. As a safety net drop() also finalizes, but that discards any I/O error.
    let mut w = Compressor::new(Vec::new(), Algorithm::Gzip, 6)?;
    w.write_all(b"...")?;
    let packed = w.finish()?;

    // Decompress from any Read.
    let mut r = Decompressor::new(&packed[..], Algorithm::Gzip)?;
    let mut out = Vec::new();
    r.read_to_end(&mut out)?;
    Ok(())
}

Supported algorithms

Algorithm Variant Wire format
Zstandard Algorithm::Zstd ZSTD frame with content size
Brotli Algorithm::Brotli Raw Brotli stream
zlib Algorithm::Zlib zlib wrapper (RFC 1950)
gzip Algorithm::Gzip gzip stream (RFC 1952)
bzip2 Algorithm::Bz2 bzip2 stream
LZ4 Algorithm::Lz4 LZ4 frame (interoperable with the lz4 CLI and .lz4 files)
XZ / LZMA Algorithm::Xz (alias Lzma) XZ stream with CRC64
Snappy Algorithm::Snappy Raw Snappy block (interoperable with reference snappy and python-snappy)
DEFLATE Algorithm::Deflate Raw DEFLATE (RFC 1951), no wrapper
LZ4 block Algorithm::Lz4Raw Raw LZ4 block, no frame or size prefix (one-shot compression, incremental decompression)
LZO1X Algorithm::Lzo Raw LZO1X block (decompression only)

Wire formats match the other compress-utils bindings and the reference tools (for example Gzip is RFC 1952, interoperable with the gzip CLI). The test suite round-trips every codec against independent pure-Rust implementations. Algorithm::Zstd.available() reports whether an algorithm was compiled in.

Raw blocks and output capacity

Raw block formats such as those inside Parquet and ORC pages don't record their decompressed size. decompress_with_capacity takes an upper bound on the output and returns only the bytes actually decoded:

let page = compress_utils::decompress_with_capacity(Algorithm::Lz4Raw, &block, page_size)?;

Without a capacity, decompress decodes Lz4Raw blocks with the incremental stream decoder, which is slower, so pass one when you know the size. Every other algorithm accepts a capacity too, which skips the size probe and decodes straight into a buffer of that size.

Compression levels

Every algorithm takes a level from 1 (fastest) to 10 (smallest), and the C core maps it to each codec's native range, so you don't need to know that Zstandard goes 1–22 and zlib goes 1–9. DEFAULT_LEVEL is 5. Codecs without levels (Snappy) ignore it.

Errors and limits

compress_utils::version();                            // e.g. "0.9.0"
compress_utils::set_max_decompressed_size(256 << 20); // cap decompress (default 1 GiB; 0 = unbounded)

// Errors carry the C status code for programmatic matching.
match compress_utils::decompress(algo, bad) {
    Err(e) if e.code == compress_utils::Status::Truncated => { /* ... */ }
    _ => {}
}

Concurrency

The one-shot compress and decompress functions are safe to call from any number of threads. A Compressor or Decompressor is not safe for concurrent use because each owns a non-thread-safe C stream handle, so use one stream per thread.

Performance

Overhead versus native C is 0.95–1.00× on compress and 0.87–1.04× on decompress, so the binding is effectively free. The one outlier is Snappy compress at 0.73×: safe Rust cannot hand out an uninitialized output buffer, so the one-shot path zeroes compress_bound bytes before the call, and on the fastest codec that memset is a visible share of the total. Compression ratio is byte-identical to every other binding. See the language comparison for methodology and the full chart.

License

MIT. See LICENSE.


Part of compress-utils. Built by Nico Dupont.