mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +00:00
Merge branch 'dir-flag'
This commit is contained in:
commit
4e731f8af3
@ -35,11 +35,11 @@ ouch decompress a.zip b.tar.gz c.tar
|
||||
ouch d a.zip
|
||||
```
|
||||
|
||||
You can redirect the decompression results to another folder with the `-o/--output` flag.
|
||||
You can redirect the decompression results to another folder with the `-d/--dir` flag.
|
||||
|
||||
```sh
|
||||
# Decompress 'summer_vacation.zip' inside of new folder 'pictures'
|
||||
ouch decompress summer_vacation.zip -o pictures
|
||||
ouch decompress summer_vacation.zip -d pictures
|
||||
```
|
||||
|
||||
### Compressing
|
||||
|
@ -47,8 +47,8 @@ pub enum Subcommand {
|
||||
files: Vec<PathBuf>,
|
||||
|
||||
/// Decompress files in a directory other than the current
|
||||
#[clap(short, long, value_hint = ValueHint::DirPath)]
|
||||
output: Option<PathBuf>,
|
||||
#[clap(short, long = "dir", value_hint = ValueHint::DirPath)]
|
||||
output_dir: Option<PathBuf>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
|
||||
|
||||
compress_result?;
|
||||
}
|
||||
Subcommand::Decompress { files, output: output_folder } => {
|
||||
Subcommand::Decompress { files, output_dir } => {
|
||||
let mut output_paths = vec![];
|
||||
let mut formats = vec![];
|
||||
|
||||
@ -173,10 +173,10 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
|
||||
}
|
||||
|
||||
// From Option<PathBuf> to Option<&Path>
|
||||
let output_folder = output_folder.as_ref().map(|path| path.as_ref());
|
||||
let output_dir = output_dir.as_ref().map(|path| path.as_ref());
|
||||
|
||||
for ((input_path, formats), file_name) in files.iter().zip(formats).zip(output_paths) {
|
||||
decompress_file(input_path, formats, output_folder, file_name, question_policy)?;
|
||||
decompress_file(input_path, formats, output_dir, file_name, question_policy)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -282,12 +282,12 @@ fn compress_files(files: Vec<PathBuf>, formats: Vec<CompressionFormat>, output_f
|
||||
|
||||
// File at input_file_path is opened for reading, example: "archive.tar.gz"
|
||||
// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order)
|
||||
// output_folder it's where the file will be decompressed to
|
||||
// output_dir it's where the file will be decompressed to
|
||||
// file_name is only used when extracting single file formats, no archive formats like .tar or .zip
|
||||
fn decompress_file(
|
||||
input_file_path: &Path,
|
||||
formats: Vec<extension::CompressionFormat>,
|
||||
output_folder: Option<&Path>,
|
||||
output_dir: Option<&Path>,
|
||||
file_name: &Path,
|
||||
question_policy: QuestionPolicy,
|
||||
) -> crate::Result<()> {
|
||||
@ -296,10 +296,10 @@ fn decompress_file(
|
||||
|
||||
// Output path is used by single file formats
|
||||
let output_path =
|
||||
if let Some(output_folder) = output_folder { output_folder.join(file_name) } else { file_name.to_path_buf() };
|
||||
if let Some(output_dir) = output_dir { output_dir.join(file_name) } else { file_name.to_path_buf() };
|
||||
|
||||
// Output folder is used by archive file formats (zip and tar)
|
||||
let output_folder = output_folder.unwrap_or_else(|| Path::new("."));
|
||||
let output_dir = output_dir.unwrap_or_else(|| Path::new("."));
|
||||
|
||||
// Zip archives are special, because they require io::Seek, so it requires it's logic separated
|
||||
// from decoder chaining.
|
||||
@ -309,10 +309,10 @@ fn decompress_file(
|
||||
//
|
||||
// Any other Zip decompression done can take up the whole RAM and freeze ouch.
|
||||
if let [Zip] = *formats.as_slice() {
|
||||
utils::create_dir_if_non_existent(output_folder)?;
|
||||
utils::create_dir_if_non_existent(output_dir)?;
|
||||
let zip_archive = zip::ZipArchive::new(reader)?;
|
||||
let _files = crate::archive::zip::unpack_archive(zip_archive, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -336,7 +336,7 @@ fn decompress_file(
|
||||
reader = chain_reader_decoder(format, reader)?;
|
||||
}
|
||||
|
||||
utils::create_dir_if_non_existent(output_folder)?;
|
||||
utils::create_dir_if_non_existent(output_dir)?;
|
||||
|
||||
match formats[0] {
|
||||
Gzip | Bzip | Lzma | Zstd => {
|
||||
@ -349,28 +349,28 @@ fn decompress_file(
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_path));
|
||||
}
|
||||
Tar => {
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
Tgz => {
|
||||
let reader = chain_reader_decoder(&Gzip, reader)?;
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
Tbz => {
|
||||
let reader = chain_reader_decoder(&Bzip, reader)?;
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
Tlzma => {
|
||||
let reader = chain_reader_decoder(&Lzma, reader)?;
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
Tzst => {
|
||||
let reader = chain_reader_decoder(&Zstd, reader)?;
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_folder, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
let _ = crate::archive::tar::unpack_archive(reader, output_dir, question_policy)?;
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
Zip => {
|
||||
eprintln!("Compressing first into .zip.");
|
||||
@ -384,9 +384,9 @@ fn decompress_file(
|
||||
io::copy(&mut reader, &mut vec)?;
|
||||
let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?;
|
||||
|
||||
let _ = crate::archive::zip::unpack_archive(zip_archive, output_folder, question_policy)?;
|
||||
let _ = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
|
||||
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_folder));
|
||||
info!("Successfully decompressed archive in {}.", nice_directory_display(output_dir));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,9 +138,7 @@ impl fmt::Display for Error {
|
||||
Error::UnknownExtensionError(_) => todo!(),
|
||||
Error::AlreadyExists => todo!(),
|
||||
Error::InvalidZipArchive(_) => todo!(),
|
||||
Error::PermissionDenied { error_title } => {
|
||||
FinalError::with_title(error_title).detail("Permission denied").to_owned()
|
||||
}
|
||||
Error::PermissionDenied { error_title } => FinalError::with_title(error_title).detail("Permission denied"),
|
||||
Error::UnsupportedZipArchive(_) => todo!(),
|
||||
Error::Custom { reason } => reason.clone(),
|
||||
};
|
||||
|
@ -182,7 +182,7 @@ fn extract_files(archive_path: &Path) -> Vec<PathBuf> {
|
||||
no: false,
|
||||
cmd: Subcommand::Decompress {
|
||||
files: vec![archive_path.to_owned()],
|
||||
output: Some(extraction_output_folder.clone()),
|
||||
output_dir: Some(extraction_output_folder.clone()),
|
||||
},
|
||||
};
|
||||
run(command, QuestionPolicy::Ask).expect("Failed to extract");
|
||||
|
@ -51,7 +51,7 @@ pub fn extract_files(archive_path: &Path) -> Vec<PathBuf> {
|
||||
no: false,
|
||||
cmd: Subcommand::Decompress {
|
||||
files: vec![archive_path.to_owned()],
|
||||
output: Some(extraction_output_folder.clone()),
|
||||
output_dir: Some(extraction_output_folder.clone()),
|
||||
},
|
||||
};
|
||||
run(command, QuestionPolicy::Ask).expect("Failed to extract");
|
||||
|
Loading…
x
Reference in New Issue
Block a user