• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate flate2;
2 
3 use flate2::write::{GzDecoder, GzEncoder};
4 use flate2::Compression;
5 use std::io;
6 use std::io::prelude::*;
7 
8 // Compress a sample string and print it after transformation.
main()9 fn 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_writer(bytes).unwrap());
14 }
15 
16 // Uncompresses a Gz Encoded vector of bytes and returns a string or error
17 // Here &[u8] implements Read
decode_writer(bytes: Vec<u8>) -> io::Result<String>18 fn decode_writer(bytes: Vec<u8>) -> io::Result<String> {
19     let mut writer = Vec::new();
20     let mut decoder = GzDecoder::new(writer);
21     decoder.write_all(&bytes[..])?;
22     decoder.try_finish()?;
23     writer = decoder.finish()?;
24     let return_string = String::from_utf8(writer).expect("String parsing error");
25     Ok(return_string)
26 }
27