Crate bzip2

Source
Expand description

Bzip compression for Rust

This library contains bindings to libbz2 to support bzip compression and decompression for Rust. The streams offered in this library are primarily found in the read and write modules. Both compressors and decompressors are available in each module depending on what operation you need.

A more low-level interface, much closer to the interface of libbz2, is available via the Compress and Decompress structs.

§Example

use std::io::{BufRead, Read, Write};
use bzip2::Compression;
use bzip2::read::{BzEncoder, BzDecoder};

// Round trip some bytes from a byte source, into a compressor, into a
// decompressor, and finally into a vector.
let data = "Hello, World!".as_bytes();
let compressor = BzEncoder::new(data, Compression::best());
let mut decompressor = BzDecoder::new(compressor);

let mut contents = String::new();
decompressor.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "Hello, World!");

§Multistreams (e.g. Wikipedia or pbzip2)

Some tools such as pbzip2 or data from sources such as Wikipedia are encoded as so called bzip2 “multistreams,” meaning they contain back to back chunks of bzip’d data. BzDecoder does not attempt to convert anything after the the first bzip chunk in the source stream. Thus, if you wish to decode all bzip chunks from the input until end of file, use MultiBzDecoder.

Protip: If you use BzDecoder to decode data and the output is incomplete and exactly 900K bytes, you probably need a MultiBzDecoder.

All methods are internally capable of working with streams that may return ErrorKind::WouldBlock when they’re not ready to perform the particular operation.

Note that care needs to be taken when using these objects, however. The Tokio runtime, in particular, requires that data is fully flushed before dropping streams. For compatibility with blocking streams all streams are flushed/written when they are dropped, and this is not always a suitable time to perform I/O. If I/O streams are flushed before drop, however, then these operations will be a noop.

Modules§

  • I/O streams for wrapping BufRead types as encoders/decoders
  • Reader-based compression/decompression streams
  • Writer-based compression/decompression streams

Structs§

  • Representation of an in-memory compression stream.
  • When compressing data, the compression level can be specified by a value in this enum.
  • Representation of an in-memory decompression stream.

Enums§

  • Possible actions to take on compression.
  • Fatal errors encountered when compressing/decompressing bytes.
  • Result of compression or decompression