···1212# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13131414[dependencies]
1515+clap = { version = "4.2.1", features = ["derive"] }
1616+tar = "0.4.38"
1717+
+5-1
README.md
···11# cmprss
2233-(UNIMPLEMENTED) A compression tool for the CLI
33+(UNIMPLEMENTED) A compression multi-tool for the CLI
44+55+## Supported formats
66+77+- tar (compress only)
+29-1
src/main.rs
···11+mod tar;
22+33+use clap::Parser;
44+use std::path::Path;
55+66+/// A compression multi-tool
77+#[derive(Parser, Debug)]
88+#[command(author, version, about, long_about = None)]
99+struct Args {
1010+ /// Input file
1111+ #[arg(index = 1)]
1212+ input: String,
1313+1414+ /// Output filename
1515+ /// If it's not provided, the extension is inferred from the compression type.
1616+ #[arg(index = 2)]
1717+ output: Option<String>,
1818+}
1919+120fn main() {
22- println!("Hello, world!");
2121+ let args = Args::parse();
2222+ let out = match args.output {
2323+ Some(file) => file,
2424+ None => {
2525+ // Append tar to the base file name as a default.
2626+ let p = Path::new(&args.input);
2727+ format!("{}{}", p.file_name().unwrap().to_str().unwrap(), ".tar")
2828+ }
2929+ };
3030+ tar::compress(Path::new(&args.input), out);
331}
+23
src/tar.rs
···11+extern crate tar;
22+33+use std::fs::File;
44+use std::path::Path;
55+use tar::Builder;
66+77+/// Compress an input file or directory into a tar archive.
88+pub fn compress<P: AsRef<Path>>(in_file: &Path, out_file: P) {
99+ let mut archive = Builder::new(File::create(out_file).unwrap());
1010+ if in_file.is_file() {
1111+ archive
1212+ .append_file(
1313+ in_file.file_name().unwrap(),
1414+ &mut File::open(in_file).unwrap(),
1515+ )
1616+ .unwrap();
1717+ } else if in_file.is_dir() {
1818+ archive
1919+ .append_dir_all(in_file.file_name().unwrap(), in_file)
2020+ .unwrap();
2121+ }
2222+ archive.finish().unwrap();
2323+}