compress-utils 0.12.2

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

[![crates.io](https://img.shields.io/crates/v/compress-utils.svg)](https://crates.io/crates/compress-utils)
[![docs.rs](https://img.shields.io/docsrs/compress-utils)](https://docs.rs/compress-utils)
[![Rust build](https://img.shields.io/github/actions/workflow/status/dupontcyborg/compress-utils/build_and_test_rust.yml?label=rust)](https://github.com/dupontcyborg/compress-utils/actions/workflows/build_and_test_rust.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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

```sh
cargo add compress-utils
```

or in `Cargo.toml`:

```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

```rust
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

<!-- bench:start -->
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/dupontcyborg/compress-utils/main/docs/benchmarks/img/readme-rust-dark.png">
  <img src="https://raw.githubusercontent.com/dupontcyborg/compress-utils/main/docs/benchmarks/img/readme-rust-light.png" alt="Compression ratio against throughput in MB/s on a log axis for compress-utils for Rust and brotli crate, bzip2 crate, flate2 (miniz_oxide), flate2 + zlib-rs, lz4_flex, snap, xz2 crate and zstd crate, one row per algorithm (zstd, brotli, zlib, xz, lz4, snappy, bz2) with compress on the left and decompress on the right, dickens one-shot on AWS c8g.xlarge (Graviton4, arm64), 2026-10-29" width="830">
</picture>

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](https://github.com/dupontcyborg/compress-utils/blob/main/docs/benchmarks/rust.md).
<!-- bench:end -->

## Supported algorithms

<!-- BEGIN:algorithms -->
<!-- Generated by tools/check-readmes.py from docs/readme/manifest.json. Edit the manifest and run `tools/check-readmes.py --fix`, not this block. -->

| 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)                                                     |
<!-- END:algorithms -->

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](https://github.com/dupontcyborg/compress-utils/blob/main/docs/bindings/rust-advanced.md#raw-blocks-and-output-capacity).

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

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

```rust
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](https://github.com/dupontcyborg/compress-utils/blob/main/docs/bindings/rust-advanced.md#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](https://github.com/dupontcyborg/compress-utils#untrusted-input) in the project README for what else to set when you decompress data you don't control.

```rust
compress_utils::version();                            // e.g. "0.12.2"
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](https://github.com/dupontcyborg/compress-utils/blob/main/LICENSE).

---

Part of [compress-utils](https://github.com/dupontcyborg/compress-utils). Built by [Nico Dupont](https://nico.codes).