Skip to content

Commit a5a38c5

Browse files
authored
Minor examlpe tweaks (#282)
* generates hello_world.txt.gz file - fixes #235 * highlights the difference between gzdecoder-bufread and -read examples
1 parent 63ecb8c commit a5a38c5

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
target
22
Cargo.lock
3+
examples/*.gz
4+
.idea

examples/gzbuilder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::fs::File;
66
use std::io;
77
use std::io::prelude::*;
88

9-
// Open file and debug print the contents compressed with gzip
9+
// Compresses content of a text file into a gzip file
1010
fn main() {
1111
sample_builder().unwrap();
1212
}
1313

1414
// GzBuilder opens a file and writes a sample string using Builder pattern
1515
fn sample_builder() -> Result<(), io::Error> {
16-
let f = File::create("examples/hello_world.gz")?;
16+
let f = File::create("examples/hello_world.txt.gz")?;
1717
let mut gz = GzBuilder::new()
1818
.filename("hello_world.txt")
1919
.comment("test file, please delete")

examples/gzdecoder-bufread.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate flate2;
22

3-
use flate2::bufread::GzDecoder;
43
use flate2::write::GzEncoder;
5-
use flate2::Compression;
4+
use flate2::{bufread, Compression};
65
use std::io;
76
use std::io::prelude::*;
87

@@ -17,7 +16,7 @@ fn main() {
1716
// Uncompresses a Gz Encoded vector of bytes and returns a string or error
1817
// Here &[u8] implements BufRead
1918
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
20-
let mut gz = GzDecoder::new(&bytes[..]);
19+
let mut gz = bufread::GzDecoder::new(&bytes[..]);
2120
let mut s = String::new();
2221
gz.read_to_string(&mut s)?;
2322
Ok(s)

examples/gzdecoder-read.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate flate2;
22

3-
use flate2::read::GzDecoder;
43
use flate2::write::GzEncoder;
5-
use flate2::Compression;
4+
use flate2::{read, Compression};
65
use std::io;
76
use std::io::prelude::*;
87

@@ -17,7 +16,7 @@ fn main() {
1716
// Uncompresses a Gz Encoded vector of bytes and returns a string or error
1817
// Here &[u8] implements Read
1918
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
20-
let mut gz = GzDecoder::new(&bytes[..]);
19+
let mut gz = read::GzDecoder::new(&bytes[..]);
2120
let mut s = String::new();
2221
gz.read_to_string(&mut s)?;
2322
Ok(s)

0 commit comments

Comments
 (0)