1# flate2 2 3[![Crates.io](https://img.shields.io/crates/v/flate2.svg?maxAge=2592000)](https://crates.io/crates/flate2) 4[![Documentation](https://docs.rs/flate2/badge.svg)](https://docs.rs/flate2) 5 6A streaming compression/decompression library DEFLATE-based streams in Rust. 7 8This crate by default uses the `miniz_oxide` crate, a port of `miniz.c` to pure 9Rust. This crate also supports other [backends](#Backends), such as the widely 10available zlib library or the high-performance zlib-ng library. 11 12Supported formats: 13 14* deflate 15* zlib 16* gzip 17 18```toml 19# Cargo.toml 20[dependencies] 21flate2 = "1.0" 22``` 23 24## Compression 25 26```rust 27use std::io::prelude::*; 28use flate2::Compression; 29use flate2::write::ZlibEncoder; 30 31fn main() { 32 let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); 33 e.write_all(b"foo"); 34 e.write_all(b"bar"); 35 let compressed_bytes = e.finish(); 36} 37``` 38 39## Decompression 40 41```rust,no_run 42use std::io::prelude::*; 43use flate2::read::GzDecoder; 44 45fn main() { 46 let mut d = GzDecoder::new("...".as_bytes()); 47 let mut s = String::new(); 48 d.read_to_string(&mut s).unwrap(); 49 println!("{}", s); 50} 51``` 52 53## Backends 54 55The default `miniz_oxide` backend has the advantage of being pure Rust, but if 56you're already using zlib with another C library, for example, you can use that 57for Rust code as well: 58 59```toml 60[dependencies] 61flate2 = { version = "1.0.17", features = ["zlib"], default-features = false } 62``` 63 64This supports either the high-performance zlib-ng backend (in zlib-compat mode) 65or the use of a shared system zlib library. To explicitly opt into the fast 66zlib-ng backend, use: 67 68```toml 69[dependencies] 70flate2 = { version = "1.0.17", features = ["zlib-ng-compat"], default-features = false } 71``` 72 73Note that if any crate in your dependency graph explicitly requests stock zlib, 74or uses libz-sys directly without `default-features = false`, you'll get stock 75zlib rather than zlib-ng. See [the libz-sys 76README](https://github.com/rust-lang/libz-sys/blob/main/README.md) for details. 77 78For compatibility with previous versions of `flate2`, the cloudflare optimized 79version of zlib is available, via the `cloudflare_zlib` feature. It's not as 80fast as zlib-ng, but it's faster than stock zlib. It requires a x86-64 CPU with 81SSE 4.2 or ARM64 with NEON & CRC. It does not support 32-bit CPUs at all and is 82incompatible with mingw. For more information check the [crate 83documentation](https://crates.io/crates/cloudflare-zlib-sys). Note that 84`cloudflare_zlib` will cause breakage if any other crate in your crate graph 85uses another version of zlib/libz. 86 87For compatibility with previous versions of `flate2`, the C version of `miniz.c` 88is still available, using the feature `miniz-sys`. 89 90# License 91 92This project is licensed under either of 93 94 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or 95 http://www.apache.org/licenses/LICENSE-2.0) 96 * MIT license ([LICENSE-MIT](LICENSE-MIT) or 97 http://opensource.org/licenses/MIT) 98 99at your option. 100 101### Contribution 102 103Unless you explicitly state otherwise, any contribution intentionally submitted 104for inclusion in this project by you, as defined in the Apache-2.0 license, 105shall be dual licensed as above, without any additional terms or conditions. 106