]>
Commit | Line | Data |
---|---|---|
7cac9316 XL |
1 | # flate2 |
2 | ||
ea8adc8c XL |
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) | |
7cac9316 | 5 | |
3dfed10e | 6 | A streaming compression/decompression library DEFLATE-based streams in Rust. |
7cac9316 | 7 | |
3dfed10e XL |
8 | This crate by default uses the `miniz_oxide` crate, a port of `miniz.c` to pure |
9 | Rust. This crate also supports other [backends](#Backends), such as the widely | |
10 | available zlib library or the high-performance zlib-ng library. | |
ff7c6d11 | 11 | |
7cac9316 XL |
12 | Supported formats: |
13 | ||
14 | * deflate | |
15 | * zlib | |
16 | * gzip | |
17 | ||
18 | ```toml | |
19 | # Cargo.toml | |
20 | [dependencies] | |
b7449926 | 21 | flate2 = "1.0" |
7cac9316 XL |
22 | ``` |
23 | ||
fe692bf9 FG |
24 | ## MSRV (Minimum Supported Rust Version) Policy |
25 | ||
26 | This crate supports the current stable and the last stable for the latest version. | |
27 | For example, if the current stable is 1.64, this crate supports 1.64 and 1.63. | |
28 | Older stables may work, but we don't guarantee these will continue to work. | |
29 | ||
7cac9316 XL |
30 | ## Compression |
31 | ||
32 | ```rust | |
7cac9316 XL |
33 | use std::io::prelude::*; |
34 | use flate2::Compression; | |
35 | use flate2::write::ZlibEncoder; | |
36 | ||
37 | fn main() { | |
ff7c6d11 | 38 | let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); |
b7449926 XL |
39 | e.write_all(b"foo"); |
40 | e.write_all(b"bar"); | |
7cac9316 XL |
41 | let compressed_bytes = e.finish(); |
42 | } | |
43 | ``` | |
44 | ||
45 | ## Decompression | |
46 | ||
47 | ```rust,no_run | |
7cac9316 XL |
48 | use std::io::prelude::*; |
49 | use flate2::read::GzDecoder; | |
50 | ||
51 | fn main() { | |
ff7c6d11 | 52 | let mut d = GzDecoder::new("...".as_bytes()); |
7cac9316 XL |
53 | let mut s = String::new(); |
54 | d.read_to_string(&mut s).unwrap(); | |
55 | println!("{}", s); | |
56 | } | |
57 | ``` | |
58 | ||
3dfed10e XL |
59 | ## Backends |
60 | ||
923072b8 FG |
61 | The default `miniz_oxide` backend has the advantage of being pure Rust. If you |
62 | want maximum performance, you can use the zlib-ng C library: | |
63 | ||
64 | ```toml | |
65 | [dependencies] | |
66 | flate2 = { version = "1.0.17", features = ["zlib-ng"], default-features = false } | |
67 | ``` | |
68 | ||
69 | Note that the `"zlib-ng"` feature works even if some other part of your crate | |
70 | graph depends on zlib. | |
71 | ||
72 | However, if you're already using another C or Rust library that depends on | |
73 | zlib, and you want to avoid including both zlib and zlib-ng, you can use that | |
5869c6ff | 74 | for Rust code as well: |
3dfed10e XL |
75 | |
76 | ```toml | |
77 | [dependencies] | |
5869c6ff | 78 | flate2 = { version = "1.0.17", features = ["zlib"], default-features = false } |
3dfed10e XL |
79 | ``` |
80 | ||
923072b8 FG |
81 | Or, if you have C or Rust code that depends on zlib and you want to use zlib-ng |
82 | via libz-sys in zlib-compat mode, use: | |
3dfed10e XL |
83 | |
84 | ```toml | |
85 | [dependencies] | |
5869c6ff | 86 | flate2 = { version = "1.0.17", features = ["zlib-ng-compat"], default-features = false } |
3dfed10e XL |
87 | ``` |
88 | ||
923072b8 FG |
89 | Note that when using the `"zlib-ng-compat"` feature, if any crate in your |
90 | dependency graph explicitly requests stock zlib, or uses libz-sys directly | |
91 | without `default-features = false`, you'll get stock zlib rather than zlib-ng. | |
92 | See [the libz-sys | |
3dfed10e | 93 | README](https://github.com/rust-lang/libz-sys/blob/main/README.md) for details. |
923072b8 | 94 | To avoid that, use the `"zlib-ng"` feature instead. |
3dfed10e | 95 | |
923072b8 | 96 | For compatibility with previous versions of `flate2`, the Cloudflare optimized |
3dfed10e | 97 | version of zlib is available, via the `cloudflare_zlib` feature. It's not as |
04454e1e | 98 | fast as zlib-ng, but it's faster than stock zlib. It requires an x86-64 CPU with |
3dfed10e XL |
99 | SSE 4.2 or ARM64 with NEON & CRC. It does not support 32-bit CPUs at all and is |
100 | incompatible with mingw. For more information check the [crate | |
101 | documentation](https://crates.io/crates/cloudflare-zlib-sys). Note that | |
102 | `cloudflare_zlib` will cause breakage if any other crate in your crate graph | |
103 | uses another version of zlib/libz. | |
104 | ||
7cac9316 XL |
105 | # License |
106 | ||
ff7c6d11 XL |
107 | This project is licensed under either of |
108 | ||
109 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or | |
923072b8 | 110 | https://www.apache.org/licenses/LICENSE-2.0) |
ff7c6d11 | 111 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
923072b8 | 112 | https://opensource.org/licenses/MIT) |
ff7c6d11 XL |
113 | |
114 | at your option. | |
115 | ||
116 | ### Contribution | |
7cac9316 | 117 | |
ff7c6d11 XL |
118 | Unless you explicitly state otherwise, any contribution intentionally submitted |
119 | for inclusion in this project by you, as defined in the Apache-2.0 license, | |
120 | shall be dual licensed as above, without any additional terms or conditions. |