use std::{fs::File, path::PathBuf};
use flate2::Compression;
use flate2::write::ZlibEncoder;
use flate2::read::ZlibDecoder;
use clap::{App, Arg};
#[allow(warnings)]
fn main() -> std::io::Result<()> {
let matches = App::new("Zlib compress/decompress")
.version("1.0")
.author("Addrew Ryan <dnrops@outlook.com>")
.about("A CLI tool for compress and decompress zlib file")
.arg(
Arg::with_name("compress")
.short("c")
.long("compress")
.value_name("STRING")
.help("compress a string in zlib file")
.takes_value(true),
)
.arg(
Arg::with_name("decompress")
.short("d")
.long("decompress")
.value_name("STRING")
.help("decompress a zlib file to data")
.takes_value(true),
)
.get_matches();
if let Some(input) = matches.value_of("compress") {
compress(input.into())?;
} else if let Some(input) = matches.value_of("decompress") {
decompress(input.into())?;
}
fn compress(path:PathBuf)->std::io::Result<()>{
let mut input_file = File::open(path)?;
let output_file = File::create("data.zlib")?;
let mut encoder = ZlibEncoder::new(output_file, Compression::default());
std::io::copy(&mut input_file, &mut encoder)?;
encoder.finish()?;
Ok(())
}
fn decompress(path:PathBuf)->std::io::Result<()>{
let input_file = File::open(path)?;
let mut output_file = File::create("data_new.txt")?;
let mut decoder = ZlibDecoder::new(input_file);
std::io::copy(&mut decoder, &mut output_file)?;
Ok(())
}
Ok(())
}