Expand description
Safe Rust bindings for libzpaq — the ZPAQ compression library by Matt Mahoney.
This crate wraps a small C++ shim (zpaq_rs_ffi.cpp) that is compiled at
build time via the cc crate. No dynamic libraries are required at
runtime; everything is statically linked.
§Compression method strings
Most functions accept a method parameter that controls the ZPAQ
compression algorithm and level. Recognised values:
| Value | Meaning |
|---|---|
"1" | Fast (level 1) |
"2" | Balanced (level 2) |
"3" | Better (level 3) |
"4" | Maximum (level 4) |
"5" | Ultra (level 5) |
"x4.3ci1" | Example explicit method string |
Higher numeric levels compress better but are slower and use more memory.
Explicit method strings (starting with x, s, i, 0–9) allow
fine-grained control; see the zpaq specification
for details.
§Feature flags
nojit— CompileslibzpaqwithNOJITdefined, disabling the JIT x86 back-end. Required on platforms without a functional x86 JIT (NetBSD, OpenBSD). Enabled automatically by the CI for those targets.
§Quick start
use zpaq_rs::{compress_to_vec, decompress_to_vec};
let original = b"hello zpaq";
let compressed = compress_to_vec(original, "1").unwrap();
let restored = decompress_to_vec(&compressed).unwrap();
assert_eq!(restored, original);For large data or streaming use cases prefer compress_stream /
decompress_stream, which accept any std::io::Read / std::io::Write.
Use compress_size / compress_size_stream when you only need the
compressed byte count and not the compressed data itself (avoids allocation).
§Performance notes
- Rust LTO applies within Rust crates; C++ LTO applies within the C++ object files. Cross-language inlining (Rust ↔ C++) is toolchain-dependent.
compress_size/compress_size_streamuse a C++-side counting writer to avoid per-byte FFI round-trips and are the fastest way to obtain $C(x)$ for information-theoretic metrics.compress_size_parallel/compress_size_stream_parallelsplit the input into ZPAQ blocks and compress them in parallel, which can be faster on multi-core machines for large inputs.
Structs§
- Archive
Entry - One in-archive file entry represented as raw bytes.
- Counting
Writer - A
Writeimplementation that discards written bytes while counting them. - Streaming
Compressor - Byte-at-a-time ZPAQ compressor that reports the running encoded bit count.
- Zpaq
Command Output - Captured output of an embedded
zpaqcommand.
Enums§
- Zpaq
Error - Errors returned by this crate.
Functions§
- archive_
append_ entries_ file - Appends raw byte entries to an archive file path without creating scratch files.
- archive_
from_ entries - Creates a ZPAQ stream archive in memory from raw byte entries.
- archive_
read_ file_ bytes - Reads the newest segment whose stored filename matches
pathfrom an archive byte slice. - archive_
read_ file_ bytes_ from_ file - Reads bytes for
pathfrom an archive file. - compress_
size - Returns the compressed size of
inputin bytes without materialising the compressed data. - compress_
size_ parallel - Returns the compressed size of
inputin bytes using multiple threads. - compress_
size_ stream - Returns the compressed size of data from
readerin bytes without materialising the compressed output. - compress_
size_ stream_ parallel - Returns the compressed size of data from
readerin bytes using multiple threads. - compress_
stream - Compresses data from
readerand writes the ZPAQ archive towriter. - compress_
to_ vec - Compresses
inputinto aVec<u8>using the given ZPAQ method string. - decompress_
size - Returns the decompressed size of the ZPAQ stream in
inputwithout materialising the output. - decompress_
size_ stream - Returns the decompressed size of the ZPAQ stream from
readerwithout materialising output. - decompress_
stream - Decompresses a ZPAQ archive from
readerand writes raw data towriter. - decompress_
to_ vec - Decompresses a complete ZPAQ stream held in
inputand returns the original data as aVec<u8>. - random_
bytes - Returns
lencryptographically strong random bytes. - sha1
- Computes the SHA-1 digest of
bytesusing the libzpaq implementation. - sha256
- Computes the SHA-256 digest of
bytesusing the libzpaq implementation. - stretch_
key - Derives a 32-byte key from
key32andsalt32using scrypt. - zpaq_
add - Equivalent of
zpaq add <archive> <inputs...> -method <method> -threads <threads>. - zpaq_
add_ archive_ size_ file - Returns the archive size (in bytes) that
zpaq addwould produce for a single file on disk. - zpaq_
command - Runs an embedded
zpaqcommand in-process and captures its output. - zpaq_
extract - Equivalent of
zpaq extract <archive> [files...]. - zpaq_
list - Equivalent of
zpaq list <archive> [files...].
Type Aliases§
- Result
- Convenience alias for
std::result::Result<T, ZpaqError>.