diff --git a/Cargo.lock b/Cargo.lock index 86caded..f1b3687 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "unzip-all" -version = "0.1.0" +version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index a7ac073..c5df0e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "unzip-all" -version = "0.1.0" +version = "0.2.0" authors = ["licsber "] edition = "2021" diff --git a/src/main.rs b/src/main.rs index f2fd11f..c2a2b00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,34 +1,9 @@ -use std::env; +mod unar; + use std::fs; -use std::io; +use std::io::Result; use std::path::{Path, PathBuf}; -use std::process::Command; - -fn unar_file(path: &Path) -> io::Result<()> { - println!("UNAR: {}", path.display()); - let parent_dir = path.parent().unwrap(); - let filename = path.file_name().unwrap(); - let output = Command::new("unar") - .current_dir(parent_dir) - .arg("-s") - .arg("-d") - .arg(filename) - .output(); - - match output { - Ok(res) => { - if !res.status.success() { - eprintln!("Error processing compressed file {:?}: {}", path, String::from_utf8_lossy(&res.stderr)); - return Err(io::ErrorKind::Other.into()); - } - } - Err(e) => { - eprintln!("Failed to execute unar for {:?}: {}", path, e); - } - } - - Ok(()) -} +use std::{env, io}; fn is_compressed(path: &Path) -> bool { if let Some(ext) = path.extension() { @@ -41,25 +16,30 @@ fn is_compressed(path: &Path) -> bool { false } -fn process_compressed_files(path: &Path) -> io::Result<()> { +fn process_compressed_files(path: &Path) -> Result<()> { if !path.is_dir() { return Err(io::Error::new(io::ErrorKind::Other, "Not a directory")); } - println!("Processing directory: {}", path.display()); + println!("Processing directory: {:?}", path); + let mut waiting_files: Vec = Vec::new(); for entry in fs::read_dir(path)? { - let entry_path = entry?.path(); + let entry_path: PathBuf = entry?.path(); if entry_path.is_dir() { process_compressed_files(&entry_path)?; } else if is_compressed(entry_path.as_path()) { - unar_file(&entry_path).expect("UNAR Error."); + waiting_files.push(entry_path); } } + for f in waiting_files { + unar::unar_file(&f).expect("UNAR Error."); + } + Ok(()) } -fn main() -> io::Result<()> { +fn main() -> Result<()> { let args: Vec = env::args().collect(); let start_path = if args.len() > 1 { @@ -69,10 +49,8 @@ fn main() -> io::Result<()> { }; if start_path.is_file() { - return unar_file(&start_path); + return unar::unar_file(&start_path); } - process_compressed_files(&start_path)?; - - Ok(()) + process_compressed_files(&start_path) } diff --git a/src/unar.rs b/src/unar.rs new file mode 100644 index 0000000..b4dd45e --- /dev/null +++ b/src/unar.rs @@ -0,0 +1,35 @@ +use std::io; +use std::path::Path; +use std::process::Command; + +pub fn unar_file(path: &Path) -> io::Result<()> { + println!("UNAR: {}", path.display()); + let parent_dir = if path.parent().unwrap().exists() { + path.parent().unwrap() + } else { + Path::new(".") + }; + + let filename = path.canonicalize()?; + + let output = Command::new("unar") + .current_dir(parent_dir) + .arg("-s") // -force-skip: skip if exists + .arg(filename) + .spawn()?.wait_with_output(); + + match output { + Ok(res) => { + if !res.status.success() { + eprintln!("Error processing compressed file {:?}: {}", path, String::from_utf8_lossy(&res.stderr)); + return Err(io::ErrorKind::Other.into()); + } + } + + Err(e) => { + eprintln!("Failed to execute unar for {:?}: {}", path, e); + } + } + + Ok(()) +}