Git Product home page Git Product logo

inflate's Introduction

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();
    }
}

inflate's People

Contributors

bvssvni avatar shnatsel avatar oyvindln avatar nwin avatar philipc avatar potocpav avatar eddyb avatar razrfalcon avatar ferjm avatar peterhj avatar

Watchers

James Cloos avatar Christopher Serr avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.