DEFLATE compression library for Rust
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Sebastian Thiel aa62dd4986
Some checks failed
CI / WebAssembly (push) Has been cancelled
CI / WebAssembly-1 (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Test-1 (push) Has been cancelled
CI / Rustfmt, Docs and Clippy (push) Has been cancelled
CI / Test-2 (push) Has been cancelled
CI / Test-3 (push) Has been cancelled
CI / Test-4 (push) Has been cancelled
CI / Test-5 (push) Has been cancelled
CI / Security Audit (push) Has been cancelled
CI / Minimum Rust compiler (push) Has been cancelled
CI / Minimum Rust compiler-1 (push) Has been cancelled
CI / Minimum Rust compiler-2 (push) Has been cancelled
CI / Minimum Rust compiler-3 (push) Has been cancelled
Merge pull request #547 from Shnatsel/fix-infinite-loop-in-gzip-header-write
Fix infinite loop in writing gzip header/footer
2026-05-03 16:46:41 +08:00
.github/workflows check that zlib-rs no longer compiles crc32fast 2026-01-15 12:45:07 +01:00
examples chore: make some documents clearer 2025-10-24 16:12:24 +08:00
fuzz initial oss-fuzz integration. (#267) 2021-04-15 10:27:57 -05:00
src Fix infinite loop in writing gzip header; add tests 2026-05-02 12:53:19 +01:00
tests Document where magic 3 in tests is coming from 2026-05-03 09:08:28 +01:00
.gitattributes Added a .gitattributes to configure how files are handled in git. 2017-03-30 00:02:27 +01:00
.gitignore Minor examlpe tweaks (#282) 2021-12-13 10:15:17 -06:00
Cargo.toml Bump miniz_oxide to 0.9 2026-03-13 10:40:17 +08:00
LICENSE-APACHE Add a dual Apache/MIT license 2014-09-03 11:20:50 -07:00
LICENSE-MIT update LICENSE-MIT 2026-01-19 02:33:34 +00:00
MAINTENANCE.md update maintenance guide with recent news 2024-08-14 07:20:59 +01:00
README.md Drop cloudflare-zlib 2026-02-16 17:03:26 +00:00

flate2

Crates.io Documentation

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

This crate by default uses the miniz_oxide crate, a port of miniz.c to pure Rust. This crate also supports other backends, such as the widely available zlib library or the high-performance zlib-ng library.

Supported formats:

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

MSRV (Minimum Supported Rust Version) Policy

This crate supports the current and previous stable versions of the Rust compiler. For example, if the current stable is 1.80, this crate supports 1.80 and 1.79.

Other compiler versions may work, but failures may not be treated as a flate2 bug.

The Cargo.toml file specifies a rust-version for which builds of the current version passed at some point. This value is indicative only, and may change at any time.

The rust-version is a best-effort measured value and is different to the MSRV. The rust-version can be incremented by a PR in order to pass tests, as long as the MSRV continues to hold. When the rust-version increases, the next release should be a minor version, to allow any affected users to pin to a previous minor version.

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

The default miniz_oxide backend has the advantage of only using safe Rust.

If you want maximum performance while still benefiting from a Rust implementation at the cost of some unsafe, you can use zlib-rs:

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

C backends

While zlib-rs is the fastest overall, the zlib-ng C library can be slightly faster in certain cases:

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

Note that the "zlib-ng" feature works even if some other part of your crate graph depends on zlib.

However, if you're already using another C or Rust library that depends on zlib, and you want to avoid including both zlib and zlib-ng, you can use that for Rust code as well:

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

Or, if you have C or Rust code that depends on zlib and you want to use zlib-ng via libz-sys in zlib-compat mode, use:

[dependencies]
flate2 = { version = "1.0.17", features = ["zlib-ng-compat"], default-features = false }

Note that when using the "zlib-ng-compat" feature, if any crate in your dependency graph explicitly requests stock zlib, or uses libz-sys directly without default-features = false, you'll get stock zlib rather than zlib-ng. See the libz-sys README for details. To avoid that, use the "zlib-ng" feature instead.

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.