Skip to main content

avalanche_types/message/
compress.rs

1use std::io::{self, Cursor, Read};
2
3use flate2::{
4    bufread::{GzDecoder, GzEncoder},
5    Compression,
6};
7
8/// Compress the input bytes.
9
10pub fn pack_gzip<S>(d: S) -> io::Result<Vec<u8>>
11where
12    S: AsRef<[u8]>,
13{
14    // ref. "golang/compress/flag.DefaultCompression" is -1 which is level 6
15    // "Compression::default()" returns 6
16    let mut gz = GzEncoder::new(Cursor::new(d), Compression::new(6));
17    let mut encoded = Vec::new();
18    gz.read_to_end(&mut encoded)?;
19    Ok(encoded)
20}
21
22/// Decompress the input bytes.
23
24pub fn unpack_gzip<S>(d: S) -> io::Result<Vec<u8>>
25where
26    S: AsRef<[u8]>,
27{
28    let mut gz = GzDecoder::new(Cursor::new(d));
29    let mut decoded = Vec::new();
30    gz.read_to_end(&mut decoded)?;
31    Ok(decoded)
32}