| 1 |
use flate2::Compression; |
| 2 |
use flate2::GzBuilder; |
| 3 |
use std::fs::File; |
| 4 |
use std::io; |
| 5 |
use std::io::prelude::*; |
| 6 |
|
| 7 |
// Compresses content of a text file into a gzip file |
| 8 |
fn main() { |
| 9 |
sample_builder().unwrap(); |
| 10 |
} |
| 11 |
|
| 12 |
// GzBuilder opens a file and writes a sample string using Builder pattern |
| 13 |
fn sample_builder() -> Result<(), io::Error> { |
| 14 |
let f = File::create("examples/hello_world.txt.gz")?; |
| 15 |
let mut gz = GzBuilder::new() |
| 16 |
.filename("hello_world.txt") |
| 17 |
.comment("test file, please delete") |
| 18 |
.write(f, Compression::default()); |
| 19 |
gz.write_all(b"hello world")?; |
| 20 |
gz.finish()?; |
| 21 |
Ok(()) |
| 22 |
} |