PistonDevelopers/inflate

Name: inflate

Owner: PistonDevelopers

Description: A pure Rust implementation of DEFLATE decompression

Created: 2015-11-10 12:51:40.0

Updated: 2018-04-24 19:10:33.0

Pushed: 2018-04-24 19:00:34.0

Homepage: null

Size: 65

Language: Rust

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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:

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

encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
decoded = inflate_bytes(&encoded).unwrap();
tln!("{}", 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”:

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

encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
mut decoder = InflateWriter::new(Vec::new());
der.write(&encoded).unwrap();
decoded = decoder.finish().unwrap();
tln!("{}", 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:

inflate::InflateStream;

data = [0x73, 0x49, 0x4d, 0xcb, 0x49, 0x2c, 0x49, 0x55, 0x00, 0x11, 0x00];
mut inflater = InflateStream::new();
mut out = Vec::<u8>::new();
mut n = 0;
e 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();
}


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.