diff options
Diffstat (limited to 'third_party/rust/flate2/examples')
23 files changed, 490 insertions, 0 deletions
diff --git a/third_party/rust/flate2/examples/compress_file.rs b/third_party/rust/flate2/examples/compress_file.rs new file mode 100644 index 0000000000..39ed8eed4e --- /dev/null +++ b/third_party/rust/flate2/examples/compress_file.rs @@ -0,0 +1,28 @@ +extern crate flate2; + +use flate2::write::GzEncoder; +use flate2::Compression; +use std::env::args; +use std::fs::File; +use std::io::copy; +use std::io::BufReader; +use std::time::Instant; + +fn main() { + if args().len() != 3 { + eprintln!("Usage: ./compress_file `source` `target`"); + return; + } + let mut input = BufReader::new(File::open(args().nth(1).unwrap()).unwrap()); + let output = File::create(args().nth(2).unwrap()).unwrap(); + let mut encoder = GzEncoder::new(output, Compression::default()); + let start = Instant::now(); + copy(&mut input, &mut encoder).unwrap(); + let output = encoder.finish().unwrap(); + println!( + "Source len: {:?}", + input.get_ref().metadata().unwrap().len() + ); + println!("Target len: {:?}", output.metadata().unwrap().len()); + println!("Elapsed: {:?}", start.elapsed()); +} diff --git a/third_party/rust/flate2/examples/deflatedecoder-bufread.rs b/third_party/rust/flate2/examples/deflatedecoder-bufread.rs new file mode 100644 index 0000000000..7d1cb6fd90 --- /dev/null +++ b/third_party/rust/flate2/examples/deflatedecoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::DeflateDecoder; +use flate2::write::DeflateEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); + e.write_all(b"Hello World").unwrap(); + let bytes = e.finish().unwrap(); + println!("{}", decode_reader(bytes).unwrap()); +} + +// Uncompresses a Deflate 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 deflater = DeflateDecoder::new(&bytes[..]); + let mut s = String::new(); + deflater.read_to_string(&mut s)?; + Ok(s) +} diff --git a/third_party/rust/flate2/examples/deflatedecoder-read.rs b/third_party/rust/flate2/examples/deflatedecoder-read.rs new file mode 100644 index 0000000000..cc5d435be3 --- /dev/null +++ b/third_party/rust/flate2/examples/deflatedecoder-read.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::read::DeflateDecoder; +use flate2::write::DeflateEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); + e.write_all(b"Hello World").unwrap(); + let bytes = e.finish().unwrap(); + println!("{}", decode_reader(bytes).unwrap()); +} + +// Uncompresses a Deflate 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 deflater = DeflateDecoder::new(&bytes[..]); + let mut s = String::new(); + deflater.read_to_string(&mut s)?; + Ok(s) +} diff --git a/third_party/rust/flate2/examples/deflatedecoder-write.rs b/third_party/rust/flate2/examples/deflatedecoder-write.rs new file mode 100644 index 0000000000..276490bac2 --- /dev/null +++ b/third_party/rust/flate2/examples/deflatedecoder-write.rs @@ -0,0 +1,26 @@ +extern crate flate2; + +use flate2::write::DeflateDecoder; +use flate2::write::DeflateEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); + e.write_all(b"Hello World").unwrap(); + let bytes = e.finish().unwrap(); + println!("{}", decode_reader(bytes).unwrap()); +} + +// Uncompresses a Deflate 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 deflater = DeflateDecoder::new(writer); + deflater.write_all(&bytes[..])?; + writer = deflater.finish()?; + let return_string = String::from_utf8(writer).expect("String parsing error"); + Ok(return_string) +} diff --git a/third_party/rust/flate2/examples/deflateencoder-bufread.rs b/third_party/rust/flate2/examples/deflateencoder-bufread.rs new file mode 100644 index 0000000000..6240f39972 --- /dev/null +++ b/third_party/rust/flate2/examples/deflateencoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::DeflateEncoder; +use flate2::Compression; +use std::fs::File; +use std::io; +use std::io::prelude::*; +use std::io::BufReader; + +// Open file and debug print the contents compressed with Deflate +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 deflater = DeflateEncoder::new(b, Compression::fast()); + let mut buffer = Vec::new(); + deflater.read_to_end(&mut buffer)?; + Ok(buffer) +} diff --git a/third_party/rust/flate2/examples/deflateencoder-read.rs b/third_party/rust/flate2/examples/deflateencoder-read.rs new file mode 100644 index 0000000000..ffb628e48e --- /dev/null +++ b/third_party/rust/flate2/examples/deflateencoder-read.rs @@ -0,0 +1,20 @@ +extern crate flate2; + +use flate2::read::DeflateEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Print the Deflate compressed representation of hello world +fn main() { + println!("{:?}", deflateencoder_read_hello_world().unwrap()); +} + +// Return a vector containing the Deflate compressed version of hello world +fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> { + let mut result = Vec::new(); + let c = b"hello world"; + let mut deflater = DeflateEncoder::new(&c[..], Compression::fast()); + deflater.read_to_end(&mut result)?; + Ok(result) +} diff --git a/third_party/rust/flate2/examples/deflateencoder-write.rs b/third_party/rust/flate2/examples/deflateencoder-write.rs new file mode 100644 index 0000000000..243b9dfdd0 --- /dev/null +++ b/third_party/rust/flate2/examples/deflateencoder-write.rs @@ -0,0 +1,12 @@ +extern crate flate2; + +use flate2::write::DeflateEncoder; +use flate2::Compression; +use std::io::prelude::*; + +// Vec<u8> implements Write to print the compressed bytes of sample string +fn main() { + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); + e.write_all(b"Hello World").unwrap(); + println!("{:?}", e.finish().unwrap()); +} diff --git a/third_party/rust/flate2/examples/gzbuilder.rs b/third_party/rust/flate2/examples/gzbuilder.rs new file mode 100644 index 0000000000..d6ec2f4ce1 --- /dev/null +++ b/third_party/rust/flate2/examples/gzbuilder.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::Compression; +use flate2::GzBuilder; +use std::fs::File; +use std::io; +use std::io::prelude::*; + +// Compresses content of a text file into a gzip file +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.txt.gz")?; + let mut gz = GzBuilder::new() + .filename("hello_world.txt") + .comment("test file, please delete") + .write(f, Compression::default()); + gz.write_all(b"hello world")?; + gz.finish()?; + Ok(()) +} diff --git a/third_party/rust/flate2/examples/gzdecoder-bufread.rs b/third_party/rust/flate2/examples/gzdecoder-bufread.rs new file mode 100644 index 0000000000..8551197393 --- /dev/null +++ b/third_party/rust/flate2/examples/gzdecoder-bufread.rs @@ -0,0 +1,23 @@ +extern crate flate2; + +use flate2::write::GzEncoder; +use flate2::{bufread, Compression}; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(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 = bufread::GzDecoder::new(&bytes[..]); + let mut s = String::new(); + gz.read_to_string(&mut s)?; + Ok(s) +} diff --git a/third_party/rust/flate2/examples/gzdecoder-read.rs b/third_party/rust/flate2/examples/gzdecoder-read.rs new file mode 100644 index 0000000000..705d28c2e1 --- /dev/null +++ b/third_party/rust/flate2/examples/gzdecoder-read.rs @@ -0,0 +1,23 @@ +extern crate flate2; + +use flate2::write::GzEncoder; +use flate2::{read, Compression}; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(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 = read::GzDecoder::new(&bytes[..]); + let mut s = String::new(); + gz.read_to_string(&mut s)?; + Ok(s) +} diff --git a/third_party/rust/flate2/examples/gzdecoder-write.rs b/third_party/rust/flate2/examples/gzdecoder-write.rs new file mode 100644 index 0000000000..766bb5e756 --- /dev/null +++ b/third_party/rust/flate2/examples/gzdecoder-write.rs @@ -0,0 +1,26 @@ +extern crate flate2; + +use flate2::write::{GzDecoder, GzEncoder}; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(b"Hello World").unwrap(); + let bytes = e.finish().unwrap(); + println!("{}", decode_writer(bytes).unwrap()); +} + +// Uncompresses a Gz Encoded vector of bytes and returns a string or error +// Here &[u8] implements Read +fn decode_writer(bytes: Vec<u8>) -> io::Result<String> { + let mut writer = Vec::new(); + let mut decoder = GzDecoder::new(writer); + decoder.write_all(&bytes[..])?; + decoder.try_finish()?; + writer = decoder.finish()?; + let return_string = String::from_utf8(writer).expect("String parsing error"); + Ok(return_string) +} diff --git a/third_party/rust/flate2/examples/gzencoder-bufread.rs b/third_party/rust/flate2/examples/gzencoder-bufread.rs new file mode 100644 index 0000000000..015ae0a82f --- /dev/null +++ b/third_party/rust/flate2/examples/gzencoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::GzEncoder; +use flate2::Compression; +use std::fs::File; +use std::io; +use std::io::prelude::*; +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) +} diff --git a/third_party/rust/flate2/examples/gzencoder-read.rs b/third_party/rust/flate2/examples/gzencoder-read.rs new file mode 100644 index 0000000000..3f1262b99a --- /dev/null +++ b/third_party/rust/flate2/examples/gzencoder-read.rs @@ -0,0 +1,20 @@ +extern crate flate2; + +use flate2::read::GzEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// 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 result = Vec::new(); + let c = b"hello world"; + let mut z = GzEncoder::new(&c[..], Compression::fast()); + z.read_to_end(&mut result)?; + Ok(result) +} diff --git a/third_party/rust/flate2/examples/gzencoder-write.rs b/third_party/rust/flate2/examples/gzencoder-write.rs new file mode 100644 index 0000000000..275b010c5f --- /dev/null +++ b/third_party/rust/flate2/examples/gzencoder-write.rs @@ -0,0 +1,12 @@ +extern crate flate2; + +use flate2::write::GzEncoder; +use flate2::Compression; +use std::io::prelude::*; + +// 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_all(b"Hello World").unwrap(); + println!("{:?}", e.finish().unwrap()); +} diff --git a/third_party/rust/flate2/examples/gzmultidecoder-bufread.rs b/third_party/rust/flate2/examples/gzmultidecoder-bufread.rs new file mode 100644 index 0000000000..c6bb2c5504 --- /dev/null +++ b/third_party/rust/flate2/examples/gzmultidecoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::MultiGzDecoder; +use flate2::write::GzEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(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) +} diff --git a/third_party/rust/flate2/examples/gzmultidecoder-read.rs b/third_party/rust/flate2/examples/gzmultidecoder-read.rs new file mode 100644 index 0000000000..7c8a8e3168 --- /dev/null +++ b/third_party/rust/flate2/examples/gzmultidecoder-read.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::read::MultiGzDecoder; +use flate2::write::GzEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(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) +} diff --git a/third_party/rust/flate2/examples/hello_world.txt b/third_party/rust/flate2/examples/hello_world.txt new file mode 100644 index 0000000000..557db03de9 --- /dev/null +++ b/third_party/rust/flate2/examples/hello_world.txt @@ -0,0 +1 @@ +Hello World diff --git a/third_party/rust/flate2/examples/zlibdecoder-bufread.rs b/third_party/rust/flate2/examples/zlibdecoder-bufread.rs new file mode 100644 index 0000000000..30f168a7b1 --- /dev/null +++ b/third_party/rust/flate2/examples/zlibdecoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::ZlibDecoder; +use flate2::write::ZlibEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); + e.write_all(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) +} diff --git a/third_party/rust/flate2/examples/zlibdecoder-read.rs b/third_party/rust/flate2/examples/zlibdecoder-read.rs new file mode 100644 index 0000000000..f7e5fb0af6 --- /dev/null +++ b/third_party/rust/flate2/examples/zlibdecoder-read.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::read::ZlibDecoder; +use flate2::write::ZlibEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); + e.write_all(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) +} diff --git a/third_party/rust/flate2/examples/zlibdecoder-write.rs b/third_party/rust/flate2/examples/zlibdecoder-write.rs new file mode 100644 index 0000000000..358e9035b1 --- /dev/null +++ b/third_party/rust/flate2/examples/zlibdecoder-write.rs @@ -0,0 +1,26 @@ +extern crate flate2; + +use flate2::write::ZlibDecoder; +use flate2::write::ZlibEncoder; +use flate2::Compression; +use std::io; +use std::io::prelude::*; + +// Compress a sample string and print it after transformation. +fn main() { + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); + e.write_all(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_all(&bytes[..])?; + writer = z.finish()?; + let return_string = String::from_utf8(writer).expect("String parsing error"); + Ok(return_string) +} diff --git a/third_party/rust/flate2/examples/zlibencoder-bufread.rs b/third_party/rust/flate2/examples/zlibencoder-bufread.rs new file mode 100644 index 0000000000..0321d8d38b --- /dev/null +++ b/third_party/rust/flate2/examples/zlibencoder-bufread.rs @@ -0,0 +1,24 @@ +extern crate flate2; + +use flate2::bufread::ZlibEncoder; +use flate2::Compression; +use std::fs::File; +use std::io; +use std::io::prelude::*; +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) +} diff --git a/third_party/rust/flate2/examples/zlibencoder-read.rs b/third_party/rust/flate2/examples/zlibencoder-read.rs new file mode 100644 index 0000000000..779eb1d31b --- /dev/null +++ b/third_party/rust/flate2/examples/zlibencoder-read.rs @@ -0,0 +1,21 @@ +extern crate flate2; + +use flate2::read::ZlibEncoder; +use flate2::Compression; +use std::fs::File; +use std::io::prelude::*; + +// 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 result = Vec::new(); + z.read_to_end(&mut result)?; + Ok(result) +} diff --git a/third_party/rust/flate2/examples/zlibencoder-write.rs b/third_party/rust/flate2/examples/zlibencoder-write.rs new file mode 100644 index 0000000000..76bcf17b17 --- /dev/null +++ b/third_party/rust/flate2/examples/zlibencoder-write.rs @@ -0,0 +1,12 @@ +extern crate flate2; + +use flate2::write::ZlibEncoder; +use flate2::Compression; +use std::io::prelude::*; + +// 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_all(b"Hello World").unwrap(); + println!("{:?}", e.finish().unwrap()); +} |