···11# cmprss
2233-(UNIMPLEMENTED) A compression multi-tool for the CLI
33+Status: pre-alpha
44+55+A compression multi-tool for the CLI.
66+77+Currently supports basic operations on `tar` archives.
4859## Supported formats
61077-- tar (compress only)
1111+- tar
+39-9
src/main.rs
···1111 #[arg(index = 1)]
1212 input: String,
13131414- /// Output filename
1414+ /// Output file/directory
1515+ ///
1516 /// If it's not provided, the extension is inferred from the compression type.
1617 #[arg(index = 2)]
1718 output: Option<String>,
1919+2020+ /// Compress the input
2121+ #[arg(short, long)]
2222+ compress: bool,
2323+2424+ /// Extract the input
2525+ #[arg(short, long)]
2626+ extract: bool,
1827}
19282020-fn main() {
2121- let args = Args::parse();
2222- let out = match args.output {
2929+/// Generates the output filename.
3030+/// This either takes the given name or guesses the name based on the extension
3131+fn output_filename(input: &Path, output: Option<String>, extension: &str) -> String {
3232+ match output {
2333 Some(file) => file,
2434 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")
3535+ format!(
3636+ "{}.{}",
3737+ input.file_name().unwrap().to_str().unwrap(),
3838+ extension
3939+ )
2840 }
2929- };
3030- tar::compress(Path::new(&args.input), out);
4141+ }
4242+}
4343+4444+fn main() {
4545+ let args = Args::parse();
4646+ let input_path = Path::new(&args.input);
4747+ if args.compress {
4848+ let out = output_filename(input_path, args.output, tar::extension());
4949+ tar::compress(input_path, out);
5050+ } else if args.extract {
5151+ tar::extract(input_path, args.output.unwrap_or(".".to_string()));
5252+ } else {
5353+ // Neither set, so infer based on filename
5454+ if input_path.extension().unwrap() == tar::extension() {
5555+ tar::extract(input_path, args.output.unwrap_or(".".to_string()));
5656+ } else {
5757+ let out = output_filename(input_path, args.output, tar::extension());
5858+ tar::compress(input_path, out);
5959+ }
6060+ }
3161}
+27-2
src/tar.rs
···2233use std::fs::File;
44use std::path::Path;
55-use tar::Builder;
55+use tar::{Archive, Builder};
66+77+/// Return the standard extension for the tar format.
88+pub fn extension() -> &'static str {
99+ "tar"
1010+}
611712/// Compress an input file or directory into a tar archive.
88-pub fn compress<P: AsRef<Path>>(in_file: &Path, out_file: P) {
1313+pub fn compress<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_file: O) {
1414+ let in_file = in_file.as_ref();
1515+ let out_file = out_file.as_ref();
1616+ println!(
1717+ "tar: Compressing {} into {}",
1818+ in_file.display(),
1919+ out_file.display()
2020+ );
921 let mut archive = Builder::new(File::create(out_file).unwrap());
1022 if in_file.is_file() {
1123 archive
···2133 }
2234 archive.finish().unwrap();
2335}
3636+3737+/// Extract the archive into the current directory
3838+pub fn extract<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_directory: O) {
3939+ let in_file = in_file.as_ref();
4040+ let out_directory = out_directory.as_ref();
4141+ println!(
4242+ "tar: Extracting {} into {}",
4343+ in_file.display(),
4444+ out_directory.display()
4545+ );
4646+ let mut archive = Archive::new(File::open(in_file).unwrap());
4747+ archive.unpack(out_directory).unwrap();
4848+}