Compression and Decompression

Compress or Decompress Data with flate2

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. It supports zlib, gzip, and raw deflate streams.

flate2 uses a pure-Rust implementation by default. Use feature flags to opt in to system zlib.

It 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. It also offers features like checksumming and error handling, making it suitable for production environments.

// //! Example of using the `flate2` crate.
// //!
// //! `flate2` is a stream compression/decompression library.
// //! It provides support for compression and decompression of DEFLATE-based
// //! streams:
// //! - the DEFLATE format itself,
// //! - the zlib format,
// //! - gzip.
// //!
// //! It supports several different backends, controlled through its features.

// use std::fs::File;
// use std::io;
// use std::io::BufReader;
// // Import the prelude for common I/O traits.
// use std::io::prelude::*;
// use std::path::Path;

// use flate2::Compression;

// /// Compress bytes.
// ///
// /// Encoders in `flate2::write` implement the `Write` interface
// /// and takes a stream of uncompressed data, writing the compressed data
// /// to the wrapped writer.
// fn compress_bytes(raw_bytes: &[u8]) -> anyhow::Result<Vec<u8>> {
//     let writer = Vec::new();
//     let mut e = flate2::write::ZlibEncoder::new(writer,
// Compression::default());     e.write_all(raw_bytes)?;
//     let compressed_bytes = e.finish()?;
//     Ok(compressed_bytes)
// }

// /// Decompress bytes.
// ///
// /// This function takes a vector of bytes, decompresses it, and returns the
// /// decompressed string.
// fn decompress_bytes(compressed_bytes: Vec<u8>) -> io::Result<String> {
//     let mut gz = flate2::read::ZlibDecoder::new(&compressed_bytes[..]);
//     let mut s = String::new();
//     gz.read_to_string(&mut s)?;
//     Ok(s)
// }

// /// Use a buffered file to compress contents into a Vec<u8>.
// /// This function reads a file, compresses its contents, and returns the
// /// compressed data in a vector.
// fn compress_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Vec<u8>> {
//     let f = File::open(path)?;
//     let b = BufReader::new(f);
//     // When read from, it reads uncompressed data from the underlying BufRead
//     // and provides the compressed data:
//     let mut z = flate2::bufread::ZlibEncoder::new(b, Compression::fast());
//     let mut buffer = Vec::new();
//     z.read_to_end(&mut buffer)?;
//     Ok(buffer)
// }

// fn main() -> anyhow::Result<()> {
//     let raw_bytes = b"Hello";
//     let compressed_bytes = compress_bytes(raw_bytes)?;
//     println!("{:?}", compressed_bytes);

//     let decompressed_string = decompress_bytes(compressed_bytes)?;

//     let compressed = compress_file("temp/hello_world.txt")?;
//     println!("{:?}", compressed);

//     Ok(())
// }

The following demonstrates asynchronous compression:

Compress or Decompress Data with zip and async-zip

zip zip-crates.io zip-github zip-lib.rs

async_zip async_zip-crates.io async_zip-github async_zip-lib.rs cat-asynchronous cat-compression

The zip crate allows you to create, open, and manipulate ZIP files, including adding, extracting, and deleting files and directories within the archive. The crate supports various features of the ZIP format, such as compression (using different algorithms), encryption, and metadata handling. It offers both streaming and buffered interfaces. The zip crate is commonly used for tasks like archiving files, distributing software, and handling data compression in applications.

async-zip is a Rust crate providing asynchronous support for reading and writing ZIP archives. Building upon the foundation of the zip crate, async-zip leverages asynchronous programming paradigms (using async/await) to enable non-blocking operations on ZIP files. This is particularly beneficial in I/O-bound contexts, such as network applications or when working with large archives, as it allows other tasks to proceed while ZIP operations are in progress. It integrates with the tokio runtime (and other async runtimes). Like its synchronous counterpart, async-zip supports various ZIP features, including compression, encryption, and metadata handling, but with the added advantage of non-blocking execution.

Related Topics

  • Filesystem.
  • Compression.