forked from rust-lang/flate2-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#111 from AndyGauge/examples
Added compression examples
- Loading branch information
Showing
17 changed files
with
645 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use std::fs::File; | ||
use flate2::GzBuilder; | ||
use flate2::Compression; | ||
|
||
// Open file and debug print the contents compressed with gzip | ||
fn main() { | ||
sample_builder().unwrap(); | ||
} | ||
|
||
// GzBuilder opens a file and writes a sample string using Builder pattern | ||
fn sample_builder() -> Result<(), io::Error> { | ||
let f = File::create("examples/hello_world.gz")?; | ||
let mut gz = GzBuilder::new() | ||
.filename("hello_world.txt") | ||
.comment("test file, please delete") | ||
.write(f, Compression::Default); | ||
gz.write(b"hello world")?; | ||
gz.finish()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::GzEncoder; | ||
use flate2::bufread::GzDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = GzEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Gz Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements BufRead | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut gz = GzDecoder::new(&bytes[..])?; | ||
let mut s = String::new(); | ||
gz.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::GzEncoder; | ||
use flate2::read::GzDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = GzEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Gz Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements Read | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut gz = GzDecoder::new(&bytes[..])?; | ||
let mut s = String::new(); | ||
gz.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::bufread::GzEncoder; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
|
||
// Open file and debug print the contents compressed with gzip | ||
fn main() { | ||
println!("{:?}", open_hello_world().unwrap()); | ||
} | ||
|
||
// Opens sample file, compresses the contents and returns a Vector or error | ||
// File wrapped in a BufReader implements Bufread | ||
fn open_hello_world() -> io::Result<Vec<u8>> { | ||
let f = File::open("examples/hello_world.txt")?; | ||
let b = BufReader::new(f); | ||
let mut gz = GzEncoder::new(b, Compression::Fast); | ||
let mut buffer = Vec::new(); | ||
gz.read_to_end(&mut buffer)?; | ||
Ok(buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::read::GzEncoder; | ||
|
||
// Print the GZ compressed representation of hello world | ||
fn main() { | ||
println!("{:?}", gzencoder_read_hello_world().unwrap()); | ||
} | ||
|
||
// Return a vector containing the GZ compressed version of hello world | ||
fn gzencoder_read_hello_world() -> io::Result<Vec<u8>> { | ||
let mut ret_vec = [0;100]; | ||
let c = b"hello world"; | ||
let mut z = GzEncoder::new(&c[..], Compression::Fast); | ||
let count = z.read(&mut ret_vec)?; | ||
Ok(ret_vec[0..count].to_vec()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use flate2::Compression; | ||
use flate2::write::GzEncoder; | ||
|
||
// Vec<u8> implements Write to print the compressed bytes of sample string | ||
fn main() { | ||
let mut e = GzEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
println!("{:?}", e.finish().unwrap()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::GzEncoder; | ||
use flate2::bufread::MultiGzDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = GzEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Gz Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements BufRead | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut gz = MultiGzDecoder::new(&bytes[..])?; | ||
let mut s = String::new(); | ||
gz.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::GzEncoder; | ||
use flate2::read::MultiGzDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = GzEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Gz Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements Read | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut gz = MultiGzDecoder::new(&bytes[..])?; | ||
let mut s = String::new(); | ||
gz.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::ZlibEncoder; | ||
use flate2::bufread::ZlibDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_bufreader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Zlib Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements BufRead | ||
fn decode_bufreader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut z = ZlibDecoder::new(&bytes[..]); | ||
let mut s = String::new(); | ||
z.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::ZlibEncoder; | ||
use flate2::read::ZlibDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Zlib Encoded vector of bytes and returns a string or error | ||
// Here &[u8] implements Read | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut z = ZlibDecoder::new(&bytes[..]); | ||
let mut s = String::new(); | ||
z.read_to_string(&mut s)?; | ||
Ok(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::write::ZlibEncoder; | ||
use flate2::write::ZlibDecoder; | ||
|
||
// Compress a sample string and print it after transformation. | ||
fn main() { | ||
let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
let bytes = e.finish().unwrap(); | ||
println!("{}", decode_reader(bytes).unwrap()); | ||
} | ||
|
||
// Uncompresses a Zlib Encoded vector of bytes and returns a string or error | ||
// Here Vec<u8> implements Write | ||
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { | ||
let mut writer = Vec::new(); | ||
let mut z = ZlibDecoder::new(writer); | ||
z.write(&bytes[..])?; | ||
writer = z.finish()?; | ||
let return_string = String::from_utf8(writer).expect("String parsing error"); | ||
Ok(return_string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use std::io; | ||
use flate2::Compression; | ||
use flate2::bufread::ZlibEncoder; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
|
||
// Open file and debug print the contents compressed with zlib | ||
fn main() { | ||
println!("{:?}", open_hello_world().unwrap()); | ||
} | ||
|
||
// Opens sample file, compresses the contents and returns a Vector or error | ||
// File wrapped in a BufReader implements Bufread | ||
fn open_hello_world() -> io::Result<Vec<u8>> { | ||
let f = File::open("examples/hello_world.txt")?; | ||
let b = BufReader::new(f); | ||
let mut z = ZlibEncoder::new(b, Compression::Fast); | ||
let mut buffer = Vec::new(); | ||
z.read_to_end(&mut buffer)?; | ||
Ok(buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use flate2::Compression; | ||
use flate2::read::ZlibEncoder; | ||
use std::fs::File; | ||
|
||
// Open file and debug print the compressed contents | ||
fn main() { | ||
println!("{:?}", open_hello_world().unwrap()); | ||
} | ||
|
||
// Opens sample file, compresses the contents and returns a Vector or error | ||
// File implements Read | ||
fn open_hello_world() -> std::io::Result<Vec<u8>> { | ||
let f = File::open("examples/hello_world.txt")?; | ||
let mut z = ZlibEncoder::new(f, Compression::Fast); | ||
let mut buffer = [0;50]; | ||
let byte_count = z.read(&mut buffer)?; | ||
Ok(buffer[0..byte_count].to_vec()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
extern crate flate2; | ||
|
||
use std::io::prelude::*; | ||
use flate2::Compression; | ||
use flate2::write::ZlibEncoder; | ||
|
||
// Vec<u8> implements Write to print the compressed bytes of sample string | ||
fn main() { | ||
let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); | ||
e.write(b"Hello World").unwrap(); | ||
println!("{:?}", e.finish().unwrap()); | ||
} |
Oops, something went wrong.