1 //! A DEFLATE-based stream compression/decompression library
3 //! This library provides support for compression and decompression of
4 //! DEFLATE-based streams:
6 //! * the DEFLATE format itself
10 //! These three formats are all closely related and largely only differ in their
11 //! headers/footers. This crate has three types in each submodule for dealing
12 //! with these three formats.
16 //! In addition to supporting three formats, this crate supports three different
17 //! backends, controlled through this crate's features:
19 //! * `default`, or `rust_backend` - this implementation uses the `miniz_oxide`
20 //! crate which is a port of `miniz.c` (below) to Rust. This feature does not
21 //! require a C compiler and only requires Rust code.
23 //! * `miniz-sys` - when enabled this feature will enable this crate to instead
24 //! use `miniz.c`, distributed with `miniz-sys`, to implement
25 //! compression/decompression.
27 //! * `zlib` - finally, this feature will enable linking against the `libz`
28 //! library, typically found on most Linux systems by default. If the library
29 //! isn't found to already be on the system it will be compiled from source
30 //! (this is a C library).
32 //! There's various tradeoffs associated with each implementation, but in
33 //! general you probably won't have to tweak the defaults. The default choice is
34 //! selected to avoid the need for a C compiler at build time. The `miniz-sys`
35 //! feature is largely a historical artifact at this point and is unlikely to be
36 //! needed, and `zlib` is often useful if you're already using `zlib` for other
37 //! C dependencies. The compression ratios and performance of each of these
38 //! feature should be roughly comparable, but you'll likely want to run your own
39 //! tests if you're curious about the performance.
43 //! This crate consists mainly of three modules, [`read`], [`write`], and
44 //! [`bufread`]. Each module contains a number of types used to encode and
45 //! decode various streams of data.
47 //! All types in the [`write`] module work on instances of [`Write`][write],
48 //! whereas all types in the [`read`] module work on instances of
49 //! [`Read`][read] and [`bufread`] works with [`BufRead`][bufread]. If you
50 //! are decoding directly from a `&[u8]`, use the [`bufread`] types.
53 //! use flate2::write::GzEncoder;
54 //! use flate2::Compression;
56 //! use std::io::prelude::*;
58 //! # fn main() { let _ = run(); }
59 //! # fn run() -> io::Result<()> {
60 //! let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
61 //! encoder.write_all(b"Example")?;
67 //! Other various types are provided at the top-level of the crate for
68 //! management and dealing with encoders/decoders. Also note that types which
69 //! operate over a specific trait often implement the mirroring trait as well.
70 //! For example a `flate2::read::DeflateDecoder<T>` *also* implements the
71 //! `Write` trait if `T: Write`. That is, the "dual trait" is forwarded directly
72 //! to the underlying object if available.
74 //! [`read`]: read/index.html
75 //! [`bufread`]: bufread/index.html
76 //! [`write`]: write/index.html
77 //! [read]: https://doc.rust-lang.org/std/io/trait.Read.html
78 //! [write]: https://doc.rust-lang.org/std/io/trait.Write.html
79 //! [bufread]: https://doc.rust-lang.org/std/io/trait.BufRead.html
83 //! This crate optionally can support async I/O streams with the [Tokio stack] via
84 //! the `tokio` feature of this crate:
86 //! [Tokio stack]: https://tokio.rs/
89 //! flate2 = { version = "0.2", features = ["tokio"] }
92 //! All methods are internally capable of working with streams that may return
93 //! [`ErrorKind::WouldBlock`] when they're not ready to perform the particular
96 //! [`ErrorKind::WouldBlock`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html
98 //! Note that care needs to be taken when using these objects, however. The
99 //! Tokio runtime, in particular, requires that data is fully flushed before
100 //! dropping streams. For compatibility with blocking streams all streams are
101 //! flushed/written when they are dropped, and this is not always a suitable
102 //! time to perform I/O. If I/O streams are flushed before drop, however, then
103 //! these operations will be a noop.
104 #![doc(html_root_url = "https://docs.rs/flate2/0.2")]
105 #![deny(missing_docs)]
106 #![deny(missing_debug_implementations)]
107 #![allow(trivial_numeric_casts)]
108 #![cfg_attr(test, deny(warnings))]
110 pub use crate::crc
::{Crc, CrcReader, CrcWriter}
;
111 pub use crate::gz
::GzBuilder
;
112 pub use crate::gz
::GzHeader
;
113 pub use crate::mem
::{Compress, CompressError, Decompress, DecompressError, Status}
;
114 pub use crate::mem
::{FlushCompress, FlushDecompress}
;
125 /// Types which operate over [`Read`] streams, both encoders and decoders for
128 /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
130 pub use crate::deflate
::read
::DeflateDecoder
;
131 pub use crate::deflate
::read
::DeflateEncoder
;
132 pub use crate::gz
::read
::GzDecoder
;
133 pub use crate::gz
::read
::GzEncoder
;
134 pub use crate::gz
::read
::MultiGzDecoder
;
135 pub use crate::zlib
::read
::ZlibDecoder
;
136 pub use crate::zlib
::read
::ZlibEncoder
;
139 /// Types which operate over [`Write`] streams, both encoders and decoders for
142 /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
144 pub use crate::deflate
::write
::DeflateDecoder
;
145 pub use crate::deflate
::write
::DeflateEncoder
;
146 pub use crate::gz
::write
::GzDecoder
;
147 pub use crate::gz
::write
::GzEncoder
;
148 pub use crate::zlib
::write
::ZlibDecoder
;
149 pub use crate::zlib
::write
::ZlibEncoder
;
152 /// Types which operate over [`BufRead`] streams, both encoders and decoders for
155 /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
157 pub use crate::deflate
::bufread
::DeflateDecoder
;
158 pub use crate::deflate
::bufread
::DeflateEncoder
;
159 pub use crate::gz
::bufread
::GzDecoder
;
160 pub use crate::gz
::bufread
::GzEncoder
;
161 pub use crate::gz
::bufread
::MultiGzDecoder
;
162 pub use crate::zlib
::bufread
::ZlibDecoder
;
163 pub use crate::zlib
::bufread
::ZlibEncoder
;
166 fn _assert_send_sync() {
167 fn _assert_send_sync
<T
: Send
+ Sync
>() {}
169 _assert_send_sync
::<read
::DeflateEncoder
<&[u8]>>();
170 _assert_send_sync
::<read
::DeflateDecoder
<&[u8]>>();
171 _assert_send_sync
::<read
::ZlibEncoder
<&[u8]>>();
172 _assert_send_sync
::<read
::ZlibDecoder
<&[u8]>>();
173 _assert_send_sync
::<read
::GzEncoder
<&[u8]>>();
174 _assert_send_sync
::<read
::GzDecoder
<&[u8]>>();
175 _assert_send_sync
::<read
::MultiGzDecoder
<&[u8]>>();
176 _assert_send_sync
::<write
::DeflateEncoder
<Vec
<u8>>>();
177 _assert_send_sync
::<write
::DeflateDecoder
<Vec
<u8>>>();
178 _assert_send_sync
::<write
::ZlibEncoder
<Vec
<u8>>>();
179 _assert_send_sync
::<write
::ZlibDecoder
<Vec
<u8>>>();
180 _assert_send_sync
::<write
::GzEncoder
<Vec
<u8>>>();
181 _assert_send_sync
::<write
::GzDecoder
<Vec
<u8>>>();
184 /// When compressing data, the compression level can be specified by a value in
186 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
187 pub struct Compression(u32);
190 /// Creates a new description of the compression level with an explicitly
191 /// specified integer.
193 /// The integer here is typically on a scale of 0-9 where 0 means "no
194 /// compression" and 9 means "take as long as you'd like".
195 pub const fn new(level
: u32) -> Compression
{
199 /// No compression is to be performed, this may actually inflate data
200 /// slightly when encoding.
201 pub const fn none() -> Compression
{
205 /// Optimize for the best speed of encoding.
206 pub const fn fast() -> Compression
{
210 /// Optimize for the size of data being encoded.
211 pub const fn best() -> Compression
{
215 /// Returns an integer representing the compression level, typically on a
217 pub fn level(&self) -> u32 {
222 impl Default
for Compression
{
223 fn default() -> Compression
{
229 fn random_bytes() -> impl Iterator
<Item
= u8> {
233 iter
::repeat(()).map(|_
| rand
::thread_rng().gen())