#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.

46 releases (14 stable)

✓ Uses Rust 2018 edition

1.0.13 Nov 11, 2019
1.0.11 Aug 14, 2019
1.0.9 Jun 21, 2019
1.0.7 Mar 14, 2019
0.1.0 Nov 27, 2014

#1 in Compression

Download history 105022/week @ 2019-11-08 106867/week @ 2019-11-15 105262/week @ 2019-11-22 99920/week @ 2019-11-29 108336/week @ 2019-12-06 106888/week @ 2019-12-13 72369/week @ 2019-12-20 66121/week @ 2019-12-27 99833/week @ 2020-01-03 112917/week @ 2020-01-10 106851/week @ 2020-01-17 112450/week @ 2020-01-24 109377/week @ 2020-01-31 107977/week @ 2020-02-07 91365/week @ 2020-02-14

426,129 downloads per month
Used in 1,654 crates (377 directly)

MIT/Apache

275KB
5K SLoC

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

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 the zlib library or miniz.c itself.

Supported formats:

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

Using zlib instead of the Rust backend:

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

Using miniz.c:

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

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);
}

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

~71–730KB
~14K SLoC