#gzip #flate #zlib #encoding

flate2

Bindings to miniz.c for DEFLATE compression and decompression exposed as Reader/Writer streams. Contains bindings for zlib, deflate, and gzip-based streams.

47 releases (15 stable)

✓ Uses Rust 2018 edition

1.0.14 Mar 17, 2020
1.0.13 Nov 11, 2019
1.0.12 Oct 2, 2019
1.0.9 Jun 21, 2019
0.1.0 Nov 27, 2014

#1 in Compression

Download history 106371/week @ 2019-12-02 108223/week @ 2019-12-09 106912/week @ 2019-12-16 63689/week @ 2019-12-23 69768/week @ 2019-12-30 107521/week @ 2020-01-06 109240/week @ 2020-01-13 111103/week @ 2020-01-20 109036/week @ 2020-01-27 112345/week @ 2020-02-03 105796/week @ 2020-02-10 96695/week @ 2020-02-17 108915/week @ 2020-02-24 106870/week @ 2020-03-02 106419/week @ 2020-03-09 110505/week @ 2020-03-16

430,294 downloads per month
Used in 1,720 crates (394 directly)

MIT/Apache

275KB
5K SLoC

Razor 3.5K SLoC // 0.3% comments C 1.5K SLoC // 0.1% comments Rust 20 SLoC

flate2

Crates.io Documentation

A streaming compression/decompression library DEFLATE-based streams in Rust.

This crate by default implemented as a wrapper around the miniz_oxide crate, a port of miniz.c to Rust. This crate can also optionally use other backends like the zlib library or miniz.c itself.

Supported formats:

  • deflate
  • zlib
  • gzip
# Cargo.toml
[dependencies]
flate2 = "1.0"

Compression

use std::io::prelude::*;
use flate2::Compression;
use flate2::write::ZlibEncoder;

fn main() {
    let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
    e.write_all(b"foo");
    e.write_all(b"bar");
    let compressed_bytes = e.finish();
}

Decompression

use std::io::prelude::*;
use flate2::read::GzDecoder;

fn main() {
    let mut d = GzDecoder::new("...".as_bytes());
    let mut s = String::new();
    d.read_to_string(&mut s).unwrap();
    println!("{}", s);
}

Backends

Using zlib instead of the (default) Rust backend:

[dependencies]
flate2 = { version = "1.0", features = ["zlib"], default-features = false }

The cloudflare optimized version of zlib is also available. While it's significantly faster it requires a x86-64 CPU with SSE 4.2 or ARM64 with NEON & CRC. It does not support 32-bit CPUs at all and is incompatible with mingw. For more information check the crate documentation.

[dependencies]
flate2 = { version = "1.0", features = ["cloudflare_zlib"], default-features = false }

Using miniz.c:

[dependencies]
flate2 = { version = "1.0", features = ["miniz-sys"], default-features = false }

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–0.9MB
~17K SLoC