this repo has no description
0
fork

Configure Feed

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

feat(tar): make tar operate on generic Readers/Writers

+20 -18
+5 -4
src/main.rs
··· 1 1 mod tar; 2 2 3 3 use clap::{Args, Parser, Subcommand}; 4 + use std::fs::File; 4 5 use std::path::Path; 5 6 6 7 /// A compression multi-tool ··· 73 74 let input_path = Path::new(&args.input); 74 75 if args.compress { 75 76 let out = output_filename(input_path, args.output, tar::EXT); 76 - tar::compress(input_path, out); 77 + tar::compress_file(input_path, File::create(out).unwrap()); 77 78 } else if args.extract { 78 - tar::extract(input_path, args.output.unwrap_or(".".to_string())); 79 + tar::extract_file(input_path, args.output.unwrap_or(".".to_string())); 79 80 } else { 80 81 // Neither is set. 81 82 // Compress by default, warn if if looks like an archive. ··· 85 86 ) 86 87 } else { 87 88 let out = output_filename(input_path, args.output, tar::EXT); 88 - tar::compress(input_path, out); 89 + tar::compress_file(input_path, File::create(out).unwrap()); 89 90 } 90 91 } 91 92 } ··· 96 97 fn command_extract(args: ExtractArgs) { 97 98 let input_path = Path::new(&args.input); 98 99 match input_path.extension().unwrap().to_str().unwrap() { 99 - tar::EXT => tar::extract(input_path, args.output.unwrap_or(".".to_string())), 100 + tar::EXT => tar::extract_file(input_path, args.output.unwrap_or(".".to_string())), 100 101 _ => println!("error: unknown format "), 101 102 } 102 103 }
+15 -14
src/tar.rs
··· 1 1 extern crate tar; 2 2 3 3 use std::fs::File; 4 + use std::io::{Read, Write}; 4 5 use std::path::Path; 5 6 use tar::{Archive, Builder}; 6 7 ··· 8 9 pub const EXT: &str = "tar"; 9 10 10 11 /// Compress an input file or directory into a tar archive. 11 - pub fn compress<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_file: O) { 12 + pub fn compress_file<I: AsRef<Path>, O: Write>(in_file: I, output: O) { 12 13 let in_file = in_file.as_ref(); 13 - let out_file = out_file.as_ref(); 14 - println!( 15 - "tar: Compressing {} into {}", 16 - in_file.display(), 17 - out_file.display() 18 - ); 19 - let mut archive = Builder::new(File::create(out_file).unwrap()); 14 + println!("tar: Compressing {}", in_file.display()); 15 + let mut archive = Builder::new(output); //File::create(out_file).unwrap()); 20 16 if in_file.is_file() { 21 17 archive 22 18 .append_file( ··· 32 28 archive.finish().unwrap(); 33 29 } 34 30 35 - /// Extract the archive into the current directory 36 - pub fn extract<I: AsRef<Path>, O: AsRef<Path>>(in_file: I, out_directory: O) { 37 - let in_file = in_file.as_ref(); 31 + /// Extract the archive file into a directory 32 + pub fn extract_file<I: AsRef<Path>, O: AsRef<Path>>(input_file: I, out_directory: O) { 33 + let input_file = input_file.as_ref(); 38 34 let out_directory = out_directory.as_ref(); 39 35 println!( 40 36 "tar: Extracting {} into {}", 41 - in_file.display(), 37 + input_file.display(), 42 38 out_directory.display() 43 39 ); 44 - let mut archive = Archive::new(File::open(in_file).unwrap()); 45 - archive.unpack(out_directory).unwrap(); 40 + extract(File::open(input_file).unwrap(), out_directory); 41 + } 42 + 43 + /// Extract the archive into a directory 44 + pub fn extract<I: Read, O: AsRef<Path>>(input: I, out_directory: O) { 45 + let mut archive = Archive::new(input); 46 + archive.unpack(out_directory.as_ref()).unwrap(); 46 47 }