compress-utils
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
or in Cargo.toml:
[]
= "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 ;
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 = decompress_with_capacity?;
Streaming (std::io::Read / Write)
use ;
use ;
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.
version; // e.g. "0.12.1"
set_max_decompressed_size; // cap decompress for every algorithm (default 1 GiB; 0 = unbounded)
decompressor.set_max_output; // a Decompressor has no limit unless set
// Errors carry the C status code for programmatic matching.
match decompress
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.