compress-utils 0.12.1

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

compress-utils

crates.io docs.rs Rust build License: MIT

One safe Rust API for Zstandard, Brotli, zlib, gzip, bzip2, LZ4, XZ/LZMA and Snappy, plus raw DEFLATE, LZ4 blocks and LZO decoding, on par with or faster than the pure-Rust flate2 (miniz_oxide) and brotli crates. Builds from bundled C sources with just a C compiler, no system libraries, all backed by one C core.

Installation

cargo add compress-utils

or in Cargo.toml:

[dependencies]
compress-utils = "0.11"

Needs Rust 1.71 or later and a C/C++ compiler (gcc, clang or MSVC); the bundled C sources are compiled by the crate's build script, so it works offline and on docs.rs, and the result is cached by Cargo. Tested on Linux, macOS and Windows.

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 otherwise has the C core grow the output buffer as it decodes (copied once into the result), 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.

Performance

compress-utils for Rust against other Rust compression libraries: speed against compression ratio.

Measured on AWS c8g.xlarge (Graviton4), dickens (10.2 MB of text), one-shot, 2026-10-29.

Full results here.

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). 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, so decompress_with_capacity takes an upper bound on the output and returns only the bytes actually decoded. More in the advanced guide.

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

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(())
}

To stop at the end of one frame and take back the bytes after it, use Decompressor::single_stream; see stopping at the end of a stream.

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

Decompression is bounded by default: output is capped at 1 GiB, so a hostile or corrupt input cannot exhaust memory. Change the cap with set_max_decompressed_size, and see the untrusted input guidance in the project README for what else to set when you decompress data you don't control.

compress_utils::version();                            // e.g. "0.12.1"
compress_utils::set_max_decompressed_size(256 << 20); // cap decompress for every algorithm (default 1 GiB; 0 = unbounded)
decompressor.set_max_output(256 << 20);               // a Decompressor has no limit unless set

// 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, so use one stream per thread.

License

MIT. See LICENSE.


Part of compress-utils. Built by Nico Dupont.