compress-utils (Rust)
Unified, safe Rust interface for eight compression algorithms — Zstandard, Brotli, zlib, gzip, bzip2, LZ4, XZ/LZMA, Snappy — over a single high-performance C core. Same API for every algorithm.
Installation
[]
= { = "https://github.com/dupontcyborg/compress-utils" }
The crate compiles the bundled C sources from source with the cc crate — 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/MSVC). The first
build compiles the codecs and is then cached by Cargo.
Quick start
use ;
let data = b"the quick brown fox ...";
let packed = compress?; // level 1 (fast)..10 (small)
let restored = decompress?;
assert_eq!;
# 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 ;
use ;
// Compress into any Write. finish() flushes and finalizes the frame — call it;
// as a safety net drop() also finalizes, but that discards any I/O error.
let mut w = new?;
w.write_all?;
let packed = w.finish?;
// Decompress from any Read.
let mut r = new?;
let mut out = Vecnew;
r.read_to_end?;
# Ok::
Algorithms
Zstd, Brotli, Zlib, Gzip, Bz2, Lz4, Xz (alias Lzma), Snappy.
Wire formats match the other compress-utils bindings and the reference tools
(e.g. 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.
Other API
version; // "0.7.1"
set_max_decompressed_size; // cap decompress (0 = unbounded)
// Errors carry the C status code for programmatic matching.
match decompress
Levels
Every algorithm takes a level from 1 (fastest) to 10 (smallest); the C core
maps it to each codec's native range. DEFAULT_LEVEL is 5. Codecs without
levels (Snappy) ignore it.
Notes
- A
Compressor/Decompressoris not safe for concurrent use; the one-shotcompress/decompressfunctions are. Use one stream per thread. - The crate is pure C — every codec (including Snappy, which uses the C
andikleen/snappy-cport) compiles to C, so there is nolibc++/libstdc++runtime dependency. - Windows is supported by the C core; the crate's source build works with MSVC. CI currently covers Linux and macOS (matching the other source bindings).