72 releases

0.13.2 Jul 5, 2024
0.13.1 Mar 27, 2024
0.13.0 Oct 11, 2023
0.12.4 Jul 19, 2023
0.1.3 Mar 2, 2016

#6 in Compression

Download history 1136154/week @ 2024-08-04 1197111/week @ 2024-08-11 1176460/week @ 2024-08-18 1217978/week @ 2024-08-25 1154582/week @ 2024-09-01 1182640/week @ 2024-09-08 1084035/week @ 2024-09-15 1177299/week @ 2024-09-22 1172614/week @ 2024-09-29 1208723/week @ 2024-10-06 1220382/week @ 2024-10-13 1280281/week @ 2024-10-20 1225639/week @ 2024-10-27 1235043/week @ 2024-11-03 1276637/week @ 2024-11-10 1295222/week @ 2024-11-17

5,099,567 downloads per month
Used in 3,421 crates (537 directly)

MIT license

190KB
3.5K SLoC

zstd

crates.io MIT licensed

Build on Linux Build on Windows Build on macOS Build on wasm

This library is a rust binding for the zstd compression library.

Documentation

1 - Add to cargo.toml

$ cargo add zstd
# Cargo.toml

[dependencies]
zstd = "0.13"

2 - Usage

This library provides Read and Write wrappers to handle (de)compression, along with convenience functions to made common tasks easier.

For instance, stream::copy_encode and stream::copy_decode are easy-to-use wrappers around std::io::copy. Check the stream example:

use std::io;

// This function use the convenient `copy_encode` method
fn compress(level: i32) {
    zstd::stream::copy_encode(io::stdin(), io::stdout(), level).unwrap();
}

// This function does the same thing, directly using an `Encoder`:
fn compress_manually(level: i32) {
    let mut encoder = zstd::stream::Encoder::new(io::stdout(), level).unwrap();
    io::copy(&mut io::stdin(), &mut encoder).unwrap();
    encoder.finish().unwrap();
}

fn decompress() {
    zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap();
}

Asynchronous support

The async-compression crate provides an async-ready integration of various compression algorithms, including zstd-rs.

Compile it yourself

zstd is included as a submodule. To get everything during your clone, use:

git clone https://github.com/gyscos/zstd-rs --recursive

Or, if you cloned it without the --recursive flag, call this from inside the repository:

git submodule update --init

Then, running cargo build should take care of building the C library and linking to it.

Build-time bindgen

This library includes a pre-generated bindings.rs file. You can also generate new bindings at build-time, using the bindgen feature:

cargo build --features bindgen

TODO

  • Benchmarks, optimizations, ...

Disclaimer

This implementation is largely inspired by bozaro's lz4-rs.

License

  • The zstd C library is under a dual BSD/GPLv2 license.
  • This zstd-rs binding library is under a MIT license.

Dependencies

~2.5–4MB
~60K SLoC