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