judge is_dir first.

This commit is contained in:
licsber 2024-09-08 10:27:01 +08:00
parent 3edc4369f4
commit a99523f4e6

View File

@ -5,33 +5,35 @@ use std::process::Command;
use std::env; use std::env;
fn process_compressed_files(path: &Path) -> io::Result<()> { fn process_compressed_files(path: &Path) -> io::Result<()> {
if path.is_dir() { if !path.is_dir() {
println!("Processing directory: {}", path.display()); return Err(io::Error::new(io::ErrorKind::Other, "Not a directory"));
for entry in fs::read_dir(path)? { }
let entry_path = entry?.path();
if entry_path.is_dir() {
process_compressed_files(&entry_path)?;
} else if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) {
if ext == "rar" || ext == "zip" {
println!("UNAR: {}", entry_path.display());
let parent_dir = entry_path.parent().unwrap();
let filename = entry_path.file_name().expect("WRONG.").to_str().unwrap();
let output = Command::new("unar")
.current_dir(parent_dir)
.arg(filename)
.output();
match output { println!("Processing directory: {}", path.display());
Ok(result) => { for entry in fs::read_dir(path)? {
if !result.status.success() { let entry_path = entry?.path();
eprintln!("Error processing compressed file {:?}: {}", entry_path, String::from_utf8_lossy(&result.stderr)); if entry_path.is_dir() {
} process_compressed_files(&entry_path)?;
} } else if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) {
Err(e) => { if ext == "rar" || ext == "zip" {
eprintln!("Failed to execute unar for {:?}: {}", entry_path, e); println!("UNAR: {}", entry_path.display());
return Err(e); let parent_dir = entry_path.parent().unwrap();
let filename = entry_path.file_name().expect("WRONG.").to_str().unwrap();
let output = Command::new("unar")
.current_dir(parent_dir)
.arg(filename)
.output();
match output {
Ok(result) => {
if !result.status.success() {
eprintln!("Error processing compressed file {:?}: {}", entry_path, String::from_utf8_lossy(&result.stderr));
} }
} }
Err(e) => {
eprintln!("Failed to execute unar for {:?}: {}", entry_path, e);
return Err(e);
}
} }
} }
} }