| 1 |
# inflate |
| 2 |
A [DEFLATE](http://www.gzip.org/zlib/rfc-deflate.html) decoder written in rust. |
| 3 |
|
| 4 |
This library provides functionality to decompress data compressed with the DEFLATE algorithm, |
| 5 |
both with and without a [zlib](https://tools.ietf.org/html/rfc1950) header/trailer. |
| 6 |
|
| 7 |
# Examples |
| 8 |
The easiest way to get `std::Vec<u8>` containing the decompressed bytes is to use either |
| 9 |
`inflate::inflate_bytes` or `inflate::inflate_bytes_zlib` (depending on whether |
| 10 |
the encoded data has zlib headers and trailers or not). The following example |
| 11 |
decodes the DEFLATE encoded string "Hello, world" and prints it: |
| 12 |
|
| 13 |
```rust |
| 14 |
use inflate::inflate_bytes; |
| 15 |
use std::str::from_utf8; |
| 16 |
|
| 17 |
let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; |
| 18 |
let decoded = inflate_bytes(&encoded).unwrap(); |
| 19 |
println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" |
| 20 |
``` |
| 21 |
|
| 22 |
If you need more flexibility, then the library also provides an implementation |
| 23 |
of `std::io::Writer` in `inflate::writer`. Below is an example using an |
| 24 |
`inflate::writer::InflateWriter` to decode the DEFLATE encoded string "Hello, world": |
| 25 |
|
| 26 |
```rust |
| 27 |
use inflate::InflateWriter; |
| 28 |
use std::io::Write; |
| 29 |
use std::str::from_utf8; |
| 30 |
|
| 31 |
let encoded = [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0]; |
| 32 |
let mut decoder = InflateWriter::new(Vec::new()); |
| 33 |
decoder.write(&encoded).unwrap(); |
| 34 |
let decoded = decoder.finish().unwrap(); |
| 35 |
println!("{}", from_utf8(&decoded).unwrap()); // prints "Hello, world" |
| 36 |
``` |
| 37 |
|
| 38 |
Finally, if you need even more flexibility, or if you only want to depend on |
| 39 |
`core`, you can use the `inflate::InflateStream` API. The below example |
| 40 |
decodes an array of DEFLATE encoded bytes: |
| 41 |
|
| 42 |
```rust |
| 43 |
use inflate::InflateStream; |
| 44 |
|
| 45 |
let data = [0x73, 0x49, 0x4d, 0xcb, 0x49, 0x2c, 0x49, 0x55, 0x00, 0x11, 0x00]; |
| 46 |
let mut inflater = InflateStream::new(); |
| 47 |
let mut out = Vec::<u8>::new(); |
| 48 |
let mut n = 0; |
| 49 |
while n < data.len() { |
| 50 |
let res = inflater.update(&data[n..]); |
| 51 |
if let Ok((num_bytes_read, result)) = res { |
| 52 |
n += num_bytes_read; |
| 53 |
out.extend(result.iter().cloned()); |
| 54 |
} else { |
| 55 |
res.unwrap(); |
| 56 |
} |
| 57 |
} |
| 58 |
``` |