#zlib #deflate #gzip #encoding

zlib_stream

An utility to decompress zlib-stream encoded WebSocket messages

2 unstable releases

0.1.0 Dec 3, 2021
0.0.2 Sep 18, 2021
0.0.1 Sep 18, 2021

#319 in WebSocket

Apache-2.0

16KB
260 lines

zlib-stream-rs

A simple utility crate to make decompressing from zlib-stream easier.

This crate is based of flate2 and their clouflare zlib backend.

Usage

  1. StreamExt using stream feature
use zlib_stream::stream::ZlibStream; 

async fn setup<V: AsRef<[u8]> + Sized, T: Stream<Item=V> + Unpin>(stream: T) {
    let mut stream = ZlibStream::new(stream);
    
    loop {
        let data: Option<Result<Vec<u8>, flate2::DecompressError>> = stream.next().await;
        do_something(data);
    }
}
  1. Barebone Implementation
use zlib_stream::{ZlibStreamDecompressor, ZlibDecompressionError};

fn worker_loop() {
    let mut decompress: ZlibStreamDecompressor = ZlibStreamDecompressor::new();
    
    loop {
        let mut frame: Vec<u8> = get_compressed_frame();
        match decompress.decompress(frame) {
            Ok(vec) => process_data(vec),
            Err(ZlibDecompressionError::NeedMoreData) => continue,
            Err(_err) => panic!("Broken frame!"),
        }
    }
}

Dependencies

~4–15MB
~172K SLoC