Module flate2

Source
Expand description

Deflate, gzip, and zlib compression.


flate2 provides compression and decompression for the DEFLATE algorithm, along with support for zlib and gzip formats. The crate offers both streaming and one-shot APIs for compression, making it suitable for a wide range of use cases.

By default, flate2 uses the miniz_oxide backend, a pure Rust implementation that requires no C compiler and uses only safe Rust code. For maximum performance, the zlib-rs backend is available, which typically outperforms C implementations.

The crate provides separate types for reading and writing compressed data: Encoder and Decoder for raw DEFLATE, GzEncoder and GzDecoder for gzip format, and ZlibEncoder and ZlibDecoder for zlib format.

§Examples

Compressing data with gzip:

use flate2::Compression;
use flate2::write::GzEncoder;
use std::io::Write;

let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(b"Hello, world!").unwrap();
let compressed = encoder.finish().unwrap();

Decompressing gzip data:

use flate2::read::GzDecoder;
use std::io::Read;

fn decompress_example(compressed: &[u8]) {
    let mut decoder = GzDecoder::new(compressed);
    let mut decompressed = String::new();
    decoder.read_to_string(&mut decompressed).unwrap();
}

Compression with the deflate algorithm:

use flate2::Compression;
use flate2::write::DeflateEncoder;
use std::io::Write;

let original = b"The quick brown fox jumps over the lazy dog";
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::best());
encoder.write_all(original).unwrap();
let compressed = encoder.finish().unwrap();

Modules§

bufread
Types which operate over BufRead streams, both encoders and decoders for various formats.
read
Types which operate over Read streams, both encoders and decoders for various formats.
write
Types which operate over Write streams, both encoders and decoders for various formats.

Structs§

Compress
Raw in-memory compression stream for blocks of data.
CompressError
Error returned when a compression object is used incorrectly or otherwise generates an error.
Compression
When compressing data, the compression level can be specified by a value in this struct.
Crc
The CRC calculated by a CrcReader.
CrcReader
A wrapper around a Read that calculates the CRC.
CrcWriter
A wrapper around a Write that calculates the CRC.
Decompress
Raw in-memory decompression stream for blocks of data.
DecompressError
Error returned when a decompression object finds that the input stream of bytes was not a valid input stream of bytes.
GzBuilder
A builder structure to create a new gzip Encoder.
GzHeader
A structure representing the header of a gzip stream.

Enums§

FlushCompress
Values which indicate the form of flushing to be used when compressing in-memory data.
FlushDecompress
Values which indicate the form of flushing to be used when decompressing in-memory data.
Status
Possible status results of compressing some data or successfully decompressing a block of data.