| 1 |
use flate2::bufread::DeflateEncoder; |
| 2 |
use flate2::Compression; |
| 3 |
use std::fs::File; |
| 4 |
use std::io; |
| 5 |
use std::io::prelude::*; |
| 6 |
use std::io::BufReader; |
| 7 |
|
| 8 |
// Open file and debug print the contents compressed with Deflate |
| 9 |
fn main() { |
| 10 |
println!("{:?}", open_hello_world().unwrap()); |
| 11 |
} |
| 12 |
|
| 13 |
// Opens sample file, compresses the contents and returns a Vector or error |
| 14 |
// File wrapped in a BufReader implements Bufread |
| 15 |
fn open_hello_world() -> io::Result<Vec<u8>> { |
| 16 |
let f = File::open("examples/hello_world.txt")?; |
| 17 |
let b = BufReader::new(f); |
| 18 |
let mut deflater = DeflateEncoder::new(b, Compression::fast()); |
| 19 |
let mut buffer = Vec::new(); |
| 20 |
deflater.read_to_end(&mut buffer)?; |
| 21 |
Ok(buffer) |
| 22 |
} |