#encoding #bzip


Bindings to libbzip2 for bzip2 compression and decompression exposed as Reader/Writer streams

19 releases

Uses old Rust 2015

0.4.3 Jun 9, 2021
0.4.2 Feb 19, 2021
0.4.1 Jul 6, 2020
0.3.3 May 3, 2018
0.0.3 Nov 19, 2014

#26 in Compression

Download history 36011/week @ 2021-04-08 33166/week @ 2021-04-15 39228/week @ 2021-04-22 36667/week @ 2021-04-29 39493/week @ 2021-05-06 35082/week @ 2021-05-13 35481/week @ 2021-05-20 33478/week @ 2021-05-27 36536/week @ 2021-06-03 40931/week @ 2021-06-10 44231/week @ 2021-06-17 42265/week @ 2021-06-24 37542/week @ 2021-07-01 41879/week @ 2021-07-08 44323/week @ 2021-07-15 43285/week @ 2021-07-22

148,911 downloads per month
Used in 560 crates (73 directly)



C 5.5K SLoC // 0.2% comments Rust 1K SLoC // 0.0% comments XSL 279 SLoC // 0.1% comments Shell 73 SLoC // 0.3% comments Perl 38 SLoC // 0.4% comments



A streaming compression/decompression library for rust with bindings to libbz2.

# Cargo.toml
bzip2 = "0.4"


This project is licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this repository by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


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 reader and writer modules. Both compressors and decompressors are available in each module depending on what operation you need.

Access to the raw decompression/compression stream is also provided through the raw module which has a much closer interface to libbz2.


use std::io::prelude::*;
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.

Async I/O

This crate optionally can support async I/O streams with the Tokio stack via the tokio feature of this crate:

bzip2 = { version = "0.4", features = ["tokio"] }

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.