this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(tar): adding tar extraction

+72 -13
+6 -2
README.md
··· 1 1 # cmprss 2 2 3 - (UNIMPLEMENTED) A compression multi-tool for the CLI 3 + Status: pre-alpha 4 + 5 + A compression multi-tool for the CLI. 6 + 7 + Currently supports basic operations on `tar` archives. 4 8 5 9 ## Supported formats 6 10 7 - - tar (compress only) 11 + - tar
+39 -9
src/main.rs
··· 11 11 #[arg(index = 1)] 12 12 input: String, 13 13 14 - /// Output filename 14 + /// Output file/directory 15 + /// 15 16 /// If it's not provided, the extension is inferred from the compression type. 16 17 #[arg(index = 2)] 17 18 output: Option<String>, 19 + 20 + /// Compress the input 21 + #[arg(short, long)] 22 + compress: bool, 23 + 24 + /// Extract the input 25 + #[arg(short, long)] 26 + extract: bool, 18 27 } 19 28 20 - fn main() { 21 - let args = Args::parse(); 22 - let out = match args.output { 29 + /// Generates the output filename. 30 + /// This either takes the given name or guesses the name based on the extension 31 + fn output_filename(input: &Path, output: Option<String>, extension: &str) -> String { 32 + match output { 23 33 Some(file) => file, 24 34 None => { 25 - // Append tar to the base file name as a default. 26 - let p = Path::new(&args.input); 27 - format!("{}{}", p.file_name().unwrap().to_str().unwrap(), ".tar") 35 + format!( 36 + "{}.{}", 37 + input.file_name().unwrap().to_str().unwrap(), 38 + extension 39 + ) 28 40 } 29 - }; 30 - tar::compress(Path::new(&args.input), out); 41 + } 42 + } 43 + 44 + fn main() { 45 + let args = Args::parse(); 46 + let input_path = Path::new(&args.input); 47 + if args.compress { 48 + let out = output_filename(input_path, args.output, tar::extension()); 49 + tar::compress(input_path, out); 50 + } else if args.extract { 51 + tar::extract(input_path, args.output.unwrap_or(".".to_string())); 52 + } else { 53 + // Neither set, so infer based on filename 54 + if input_path.extension().unwrap() == tar::extension() { 55 + tar::extract(input_path, args.output.unwrap_or(".".to_string())); 56 + } else { 57 + let out = output_filename(input_path, args.output, tar::extension()); 58 + tar::compress(input_path, out); 59 + } 60 + } 31 61 }
+27 -2
src/tar.rs
··· 2 2 3 3 use std::fs::File; 4 4 use std::path::Path; 5 - use tar::Builder; 5 + use tar::{Archive, Builder}; 6 + 7 + /// Return the standard extension for the tar format. 8 + pub fn extension() -> &'static str { 9 + "tar" 10 + } 6 11 7 12 /// Compress an input file or directory into a tar archive. 8 - pub fn compress<P: AsRef<Path>>(in_file: &Path, out_file: P) { 13 + pub fn compress<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_file: O) { 14 + let in_file = in_file.as_ref(); 15 + let out_file = out_file.as_ref(); 16 + println!( 17 + "tar: Compressing {} into {}", 18 + in_file.display(), 19 + out_file.display() 20 + ); 9 21 let mut archive = Builder::new(File::create(out_file).unwrap()); 10 22 if in_file.is_file() { 11 23 archive ··· 21 33 } 22 34 archive.finish().unwrap(); 23 35 } 36 + 37 + /// Extract the archive into the current directory 38 + pub fn extract<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_directory: O) { 39 + let in_file = in_file.as_ref(); 40 + let out_directory = out_directory.as_ref(); 41 + println!( 42 + "tar: Extracting {} into {}", 43 + in_file.display(), 44 + out_directory.display() 45 + ); 46 + let mut archive = Archive::new(File::open(in_file).unwrap()); 47 + archive.unpack(out_directory).unwrap(); 48 + }