Deflate

Compress then Decompress

flate2 flate2-crates.io flate2-github flate2-lib.rs cat-api-bindings cat-compression

flate2 provides DEFLATE compression and decompression, exposed as Read/BufRead/Write streams. flate2 uses a pure-Rust implementation by default. Use feature flags to opt in to system zlib. Supports zlib, gzip, and raw deflate streams.

The flate2 crate in Rust provides bindings to the zlib, zlib-ng, and minizlib compression libraries. It offers a comprehensive set of functionalities for working with various compression formats, including gzip, deflate, and zlib. flate2 provides both high-level and low-level interfaces. It supports both compression and decompression operations and can be used with various I/O streams, making it versatile for different use cases, such as file compression, network protocols, and in-memory data manipulation. The crate leverages the underlying C libraries for optimal performance. It also offers features like checksumming and error handling, making it suitable for production environments.

Compress, then decompress data:

use std::io::prelude::*;

use flate2::Compression;
use flate2::read::GzDecoder;
use flate2::write::ZlibEncoder;

fn compress() -> anyhow::Result<()> {
    let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
    e.write_all(b"foo")?;
    e.write_all(b"bar")?;
    let _compressed_bytes = e.finish();
    Ok(())
}

fn decompress() -> anyhow::Result<()> {
    let mut d = GzDecoder::new("...".as_bytes());
    let mut s = String::new();
    d.read_to_string(&mut s)?;
    println!("{}", s);
    Ok(())
}

fn main() -> anyhow::Result<()> {
    compress()?;
    decompress()?;
    Ok(())
}

Related Topics

  • Filesystem.
  • Compression.