• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate flate2;
2 
3 use flate2::write::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 Vec<u8> implements Write
decode_reader(bytes: Vec<u8>) -> io::Result<String>19 fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
20     let mut writer = Vec::new();
21     let mut deflater = DeflateDecoder::new(writer);
22     deflater.write_all(&bytes[..])?;
23     writer = deflater.finish()?;
24     let return_string = String::from_utf8(writer).expect("String parsing error");
25     Ok(return_string)
26 }
27