• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate flate2;
2 
3 use flate2::read::DeflateDecoder;
4 use flate2::write::DeflateEncoder;
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()10 fn main() {
11     let mut e = DeflateEncoder::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 Deflate Encoded vector of bytes and returns a string or error
18 // Here &[u8] implements Read
decode_reader(bytes: Vec<u8>) -> io::Result<String>19 fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
20     let mut deflater = DeflateDecoder::new(&bytes[..]);
21     let mut s = String::new();
22     deflater.read_to_string(&mut s)?;
23     Ok(s)
24 }
25