Skip to content

Commit

Permalink
Merge pull request rust-lang#111 from AndyGauge/examples
Browse files Browse the repository at this point in the history
Added compression examples
  • Loading branch information
alexcrichton authored May 12, 2017
2 parents 42156e6 + 4f949ba commit a73c33e
Show file tree
Hide file tree
Showing 17 changed files with 645 additions and 7 deletions.
24 changes: 24 additions & 0 deletions examples/gzbuilder.rs
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(())
}
24 changes: 24 additions & 0 deletions examples/gzdecoder-bufread.rs
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)
}
24 changes: 24 additions & 0 deletions examples/gzdecoder-read.rs
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)
}
24 changes: 24 additions & 0 deletions examples/gzencoder-bufread.rs
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)
}
20 changes: 20 additions & 0 deletions examples/gzencoder-read.rs
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())
}
12 changes: 12 additions & 0 deletions examples/gzencoder-write.rs
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());
}
24 changes: 24 additions & 0 deletions examples/gzmultidecoder-bufread.rs
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)
}
24 changes: 24 additions & 0 deletions examples/gzmultidecoder-read.rs
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)
}
1 change: 1 addition & 0 deletions examples/hello_world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
24 changes: 24 additions & 0 deletions examples/zlibdecoder-bufread.rs
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)
}
24 changes: 24 additions & 0 deletions examples/zlibdecoder-read.rs
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)
}
26 changes: 26 additions & 0 deletions examples/zlibdecoder-write.rs
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)
}
24 changes: 24 additions & 0 deletions examples/zlibencoder-bufread.rs
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)
}
21 changes: 21 additions & 0 deletions examples/zlibencoder-read.rs
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())
}
12 changes: 12 additions & 0 deletions examples/zlibencoder-write.rs
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());
}
Loading

0 comments on commit a73c33e

Please sign in to comment.