diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/flate2/examples | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/flate2/examples')
25 files changed, 469 insertions, 0 deletions
diff --git a/vendor/flate2/examples/compress_file.rs b/vendor/flate2/examples/compress_file.rs new file mode 100644 index 0000000..be4b81e --- /dev/null +++ b/vendor/flate2/examples/compress_file.rs @@ -0,0 +1,26 @@ +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/vendor/flate2/examples/decompress_file.rs b/vendor/flate2/examples/decompress_file.rs new file mode 100644 index 0000000..6b2ede9 --- /dev/null +++ b/vendor/flate2/examples/decompress_file.rs @@ -0,0 +1,23 @@ +use flate2::bufread; +use std::env::args; +use std::fs::File; +use std::io::copy; +use std::io::BufReader; +use std::time::Instant; + +fn main() { + // E.g. `cargo run --example decompress_file examples/hello_world.txt.gz hello_world.txt` + if args().len() != 3 { + eprintln!("Usage: ./decompress_file `source` `target`"); + return; + } + let input = BufReader::new(File::open(args().nth(1).unwrap()).unwrap()); + let mut output = File::create(args().nth(2).unwrap()).unwrap(); + let source_len = input.get_ref().metadata().unwrap().len(); + let start = Instant::now(); + let mut decoder = bufread::GzDecoder::new(input); + copy(&mut decoder, &mut output).unwrap(); + println!("Source len: {:?}", source_len); + println!("Target len: {:?}", output.metadata().unwrap().len()); + println!("Elapsed: {:?}", start.elapsed()); +} diff --git a/vendor/flate2/examples/deflatedecoder-bufread.rs b/vendor/flate2/examples/deflatedecoder-bufread.rs new file mode 100644 index 0000000..6b8e7fe --- /dev/null +++ b/vendor/flate2/examples/deflatedecoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/deflatedecoder-read.rs b/vendor/flate2/examples/deflatedecoder-read.rs new file mode 100644 index 0000000..1bc83bc --- /dev/null +++ b/vendor/flate2/examples/deflatedecoder-read.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/deflatedecoder-write.rs b/vendor/flate2/examples/deflatedecoder-write.rs new file mode 100644 index 0000000..b9ba7a3 --- /dev/null +++ b/vendor/flate2/examples/deflatedecoder-write.rs @@ -0,0 +1,24 @@ +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/vendor/flate2/examples/deflateencoder-bufread.rs b/vendor/flate2/examples/deflateencoder-bufread.rs new file mode 100644 index 0000000..bff1848 --- /dev/null +++ b/vendor/flate2/examples/deflateencoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/deflateencoder-read.rs b/vendor/flate2/examples/deflateencoder-read.rs new file mode 100644 index 0000000..366c60c --- /dev/null +++ b/vendor/flate2/examples/deflateencoder-read.rs @@ -0,0 +1,18 @@ +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/vendor/flate2/examples/deflateencoder-write.rs b/vendor/flate2/examples/deflateencoder-write.rs new file mode 100644 index 0000000..f998e1a --- /dev/null +++ b/vendor/flate2/examples/deflateencoder-write.rs @@ -0,0 +1,10 @@ +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/vendor/flate2/examples/gzbuilder.rs b/vendor/flate2/examples/gzbuilder.rs new file mode 100644 index 0000000..c072508 --- /dev/null +++ b/vendor/flate2/examples/gzbuilder.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/gzdecoder-bufread.rs b/vendor/flate2/examples/gzdecoder-bufread.rs new file mode 100644 index 0000000..06d0448 --- /dev/null +++ b/vendor/flate2/examples/gzdecoder-bufread.rs @@ -0,0 +1,21 @@ +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/vendor/flate2/examples/gzdecoder-read.rs b/vendor/flate2/examples/gzdecoder-read.rs new file mode 100644 index 0000000..fbd05be --- /dev/null +++ b/vendor/flate2/examples/gzdecoder-read.rs @@ -0,0 +1,21 @@ +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/vendor/flate2/examples/gzdecoder-write.rs b/vendor/flate2/examples/gzdecoder-write.rs new file mode 100644 index 0000000..6cee8f8 --- /dev/null +++ b/vendor/flate2/examples/gzdecoder-write.rs @@ -0,0 +1,24 @@ +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/vendor/flate2/examples/gzencoder-bufread.rs b/vendor/flate2/examples/gzencoder-bufread.rs new file mode 100644 index 0000000..2214e2d --- /dev/null +++ b/vendor/flate2/examples/gzencoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/gzencoder-read.rs b/vendor/flate2/examples/gzencoder-read.rs new file mode 100644 index 0000000..ee13848 --- /dev/null +++ b/vendor/flate2/examples/gzencoder-read.rs @@ -0,0 +1,18 @@ +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/vendor/flate2/examples/gzencoder-write.rs b/vendor/flate2/examples/gzencoder-write.rs new file mode 100644 index 0000000..cee01d1 --- /dev/null +++ b/vendor/flate2/examples/gzencoder-write.rs @@ -0,0 +1,10 @@ +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/vendor/flate2/examples/gzmultidecoder-bufread.rs b/vendor/flate2/examples/gzmultidecoder-bufread.rs new file mode 100644 index 0000000..692b6f5 --- /dev/null +++ b/vendor/flate2/examples/gzmultidecoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/gzmultidecoder-read.rs b/vendor/flate2/examples/gzmultidecoder-read.rs new file mode 100644 index 0000000..c59561d --- /dev/null +++ b/vendor/flate2/examples/gzmultidecoder-read.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/hello_world.txt b/vendor/flate2/examples/hello_world.txt new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/vendor/flate2/examples/hello_world.txt @@ -0,0 +1 @@ +Hello World diff --git a/vendor/flate2/examples/hello_world.txt.gz b/vendor/flate2/examples/hello_world.txt.gz Binary files differnew file mode 100644 index 0000000..bab9799 --- /dev/null +++ b/vendor/flate2/examples/hello_world.txt.gz diff --git a/vendor/flate2/examples/zlibdecoder-bufread.rs b/vendor/flate2/examples/zlibdecoder-bufread.rs new file mode 100644 index 0000000..82620c5 --- /dev/null +++ b/vendor/flate2/examples/zlibdecoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/zlibdecoder-read.rs b/vendor/flate2/examples/zlibdecoder-read.rs new file mode 100644 index 0000000..3d4cda7 --- /dev/null +++ b/vendor/flate2/examples/zlibdecoder-read.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/zlibdecoder-write.rs b/vendor/flate2/examples/zlibdecoder-write.rs new file mode 100644 index 0000000..7f5afbe --- /dev/null +++ b/vendor/flate2/examples/zlibdecoder-write.rs @@ -0,0 +1,24 @@ +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/vendor/flate2/examples/zlibencoder-bufread.rs b/vendor/flate2/examples/zlibencoder-bufread.rs new file mode 100644 index 0000000..e5e17f5 --- /dev/null +++ b/vendor/flate2/examples/zlibencoder-bufread.rs @@ -0,0 +1,22 @@ +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/vendor/flate2/examples/zlibencoder-read.rs b/vendor/flate2/examples/zlibencoder-read.rs new file mode 100644 index 0000000..f833a50 --- /dev/null +++ b/vendor/flate2/examples/zlibencoder-read.rs @@ -0,0 +1,19 @@ +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/vendor/flate2/examples/zlibencoder-write.rs b/vendor/flate2/examples/zlibencoder-write.rs new file mode 100644 index 0000000..3e3548f --- /dev/null +++ b/vendor/flate2/examples/zlibencoder-write.rs @@ -0,0 +1,10 @@ +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()); +} |