1 extern crate flate2; 2 3 use flate2::write::GzEncoder; 4 use flate2::{bufread, Compression}; 5 use std::io; 6 use std::io::prelude::*; 7 8 // Compress a sample string and print it after transformation. main()9fn main() { 10 let mut e = GzEncoder::new(Vec::new(), Compression::default()); 11 e.write_all(b"Hello World").unwrap(); 12 let bytes = e.finish().unwrap(); 13 println!("{}", decode_reader(bytes).unwrap()); 14 } 15 16 // Uncompresses a Gz Encoded vector of bytes and returns a string or error 17 // Here &[u8] implements BufRead decode_reader(bytes: Vec<u8>) -> io::Result<String>18fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { 19 let mut gz = bufread::GzDecoder::new(&bytes[..]); 20 let mut s = String::new(); 21 gz.read_to_string(&mut s)?; 22 Ok(s) 23 } 24