14 releases

Uses old Rust 2015

0.4.5 Feb 13, 2019
0.4.4 Dec 10, 2018
0.4.3 Jun 28, 2018
0.4.0 Mar 27, 2018
0.1.0 Nov 10, 2015

#27 in Compression

Download history 24483/week @ 2024-03-14 19484/week @ 2024-03-21 49774/week @ 2024-03-28 19674/week @ 2024-04-04 23181/week @ 2024-04-11 22205/week @ 2024-04-18 23586/week @ 2024-04-25 17974/week @ 2024-05-02 18002/week @ 2024-05-09 32116/week @ 2024-05-16 29081/week @ 2024-05-23 23353/week @ 2024-05-30 24718/week @ 2024-06-06 20991/week @ 2024-06-13 21215/week @ 2024-06-20 19109/week @ 2024-06-27

89,462 downloads per month
Used in 214 crates (39 directly)

MIT license

66KB
1K SLoC

inflate

A DEFLATE decoder written in rust.

This library provides functionality to decompress data compressed with the DEFLATE algorithm, both with and without a zlib header/trailer.

Examples

The easiest way to get std::Vec<u8> containing the decompressed bytes is to use either inflate::inflate_bytes or inflate::inflate_bytes_zlib (depending on whether the encoded data has zlib headers and trailers or not). The following example decodes the DEFLATE encoded string "Hello, world" and prints it:

use inflate::inflate_bytes;
use std::str::from_utf8;

let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
let decoded = inflate_bytes(&encoded).unwrap();
println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world"

If you need more flexibility, then the library also provides an implementation of std::io::Writer in inflate::writer. Below is an example using an inflate::writer::InflateWriter to decode the DEFLATE encoded string "Hello, world":

use inflate::InflateWriter;
use std::io::Write;
use std::str::from_utf8;

let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
let mut decoder = InflateWriter::new(Vec::new());
decoder.write(&encoded).unwrap();
let decoded = decoder.finish().unwrap();
println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world"

Finally, if you need even more flexibility, or if you only want to depend on core, you can use the inflate::InflateStream API. The below example decodes an array of DEFLATE encoded bytes:

use inflate::InflateStream;

let data = [0x73, 0x49, 0x4d, 0xcb, 0x49, 0x2c, 0x49, 0x55, 0x00, 0x11, 0x00];
let mut inflater = InflateStream::new();
let mut out = Vec::<u8>::new();
let mut n = 0;
while n < data.len() {
    let res = inflater.update(&data[n..]);
    if let Ok((num_bytes_read, result)) = res {
        n += num_bytes_read;
        out.extend(result.iter().cloned());
    } else {
        res.unwrap();
    }
}

Dependencies