this repo has no description
0
fork

Configure Feed

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

feat: allow multiple input files to compression

+52 -23
+24 -4
src/gzip.rs
··· 27 27 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 28 28 match (input, output) { 29 29 (CmprssInput::Path(in_path), CmprssOutput::Path(out_path)) => { 30 - self.compress_internal(File::open(in_path)?, File::create(out_path)?) 30 + let mut encoder = GzEncoder::new( 31 + File::create(out_path)?, 32 + Compression::new(self.compression_level), 33 + ); 34 + for x in in_path { 35 + std::io::copy(&mut File::open(x)?, &mut encoder)?; 36 + } 37 + encoder.finish()?; 38 + Ok(()) 31 39 } 32 40 (CmprssInput::Path(in_path), CmprssOutput::Pipe(out_pipe)) => { 33 - self.compress_internal(File::open(in_path)?, out_pipe) 41 + let mut encoder = 42 + GzEncoder::new(out_pipe, Compression::new(self.compression_level)); 43 + for x in in_path { 44 + std::io::copy(&mut File::open(x)?, &mut encoder)?; 45 + } 46 + encoder.finish()?; 47 + Ok(()) 34 48 } 35 49 (CmprssInput::Pipe(in_pipe), CmprssOutput::Path(out_path)) => { 36 50 self.compress_internal(in_pipe, File::create(out_path)?) ··· 44 58 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 45 59 match (input, output) { 46 60 (CmprssInput::Path(in_path), CmprssOutput::Path(out_path)) => { 47 - self.extract_internal(File::open(in_path)?, File::create(out_path)?) 61 + if in_path.len() > 1 { 62 + return cmprss_error("only 1 archive can be extracted at a time"); 63 + } 64 + self.extract_internal(File::open(in_path[0])?, File::create(out_path)?) 48 65 } 49 66 (CmprssInput::Path(in_path), CmprssOutput::Pipe(out_pipe)) => { 50 - self.extract_internal(File::open(in_path)?, out_pipe) 67 + if in_path.len() > 1 { 68 + return cmprss_error("only 1 archive can be extracted at a time"); 69 + } 70 + self.extract_internal(File::open(in_path[0])?, out_pipe) 51 71 } 52 72 (CmprssInput::Pipe(in_pipe), CmprssOutput::Path(out_path)) => { 53 73 self.extract_internal(in_pipe, File::create(out_path)?)
+7 -5
src/main.rs
··· 132 132 false => CmprssInput::Pipe(std::io::stdin()), 133 133 true => { 134 134 // stdin isn't a pipe, need to read from a file 135 - CmprssInput::Path(Path::new(get_input_filename(&args.input)?)) 135 + CmprssInput::Path(vec![Path::new(get_input_filename(&args.input)?)]) 136 136 } 137 137 }; 138 138 // Output prefers the stdout if we're piping, and falls back to piping to a file. ··· 164 164 // Neither compress nor extract is specified. 165 165 // Compress by default, warn if if looks like an archive. 166 166 match &input { 167 - CmprssInput::Path(path) => { 168 - if let Some(ext) = path.extension() { 169 - if ext == compressor.extension() { 170 - return cmprss_error( 167 + CmprssInput::Path(paths) => { 168 + for x in paths { 169 + if let Some(ext) = x.extension() { 170 + if ext == compressor.extension() { 171 + return cmprss_error( 171 172 &format!("error: input appears to already be a {} archive, exiting. Use '--compress' if needed.", compressor.name())); 173 + } 172 174 } 173 175 } 174 176 compressor.compress(input, output)?;
+18 -13
src/tar.rs
··· 37 37 38 38 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 39 39 match input { 40 - CmprssInput::Path(path) => { 41 - self.extract_internal(Archive::new(File::open(path)?), output) 40 + CmprssInput::Path(paths) => { 41 + if paths.len() > 1 { 42 + return cmprss_error("only 1 archive can be extracted at a time"); 43 + } 44 + self.extract_internal(Archive::new(File::open(paths[0])?), output) 42 45 } 43 46 CmprssInput::Pipe(pipe) => self.extract_internal(Archive::new(pipe), output), 44 47 } ··· 67 70 input: CmprssInput, 68 71 mut archive: Builder<W>, 69 72 ) -> Result<(), io::Error> { 70 - let in_file = match input { 71 - CmprssInput::Path(path) => path, 73 + let input_files = match input { 74 + CmprssInput::Path(paths) => paths, 72 75 CmprssInput::Pipe(_) => { 73 76 return cmprss_error("error: tar does not support stdin as input") 74 77 } 75 78 }; 76 - if in_file.is_file() { 77 - archive.append_file(in_file.file_name().unwrap(), &mut File::open(in_file)?)?; 78 - } else if in_file.is_dir() { 79 - archive.append_dir_all(in_file.file_name().unwrap(), in_file)?; 80 - } else { 81 - return Err(io::Error::new( 82 - io::ErrorKind::InvalidInput, 83 - "unknown file type", 84 - )); 79 + for in_file in input_files { 80 + if in_file.is_file() { 81 + archive.append_file(in_file.file_name().unwrap(), &mut File::open(in_file)?)?; 82 + } else if in_file.is_dir() { 83 + archive.append_dir_all(in_file.file_name().unwrap(), in_file)?; 84 + } else { 85 + return Err(io::Error::new( 86 + io::ErrorKind::InvalidInput, 87 + "unknown file type", 88 + )); 89 + } 85 90 } 86 91 archive.finish() 87 92 }
+3 -1
src/utils.rs
··· 53 53 /// Defines the possible inputs of a compressor 54 54 // TODO: Implement fmt for CmprssInput/CmprssOutput 55 55 pub enum CmprssInput<'a> { 56 - Path(&'a Path), 56 + /// Path(s) to the input files. 57 + Path(Vec<&'a Path>), 58 + /// Input pipe 57 59 Pipe(std::io::Stdin), 58 60 } 59 61