using parent dir as pwd.

This commit is contained in:
licsber 2024-09-08 00:13:55 +08:00
parent c870b16634
commit 5c4a6b6bf1

View File

@ -6,30 +6,33 @@ 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!("Path: {}", path.display()); println!("Processing directory: {}", path.display());
for entry in fs::read_dir(path)? { for entry in fs::read_dir(path)? {
let entry_path = fs::canonicalize(entry?.path())?; let entry_path = entry?.path();
process_compressed_files(&entry_path)?; if entry_path.is_dir() {
} process_compressed_files(&entry_path)?;
} else { } else if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) {
if path.extension().map_or(false, |ext| if ext == "rar" || ext == "zip" {
ext == "rar" || ext == "zip", println!("UNAR: {}", entry_path.display());
) { let parent_dir = entry_path.parent().unwrap();
println!("UNAR: {}", path.display()); let filename = entry_path.file_name().to_str().unwrap();
let output = Command::new("unar") let output = Command::new("unar")
.current_dir(path) .current_dir(parent_dir)
.arg(path) .arg(filename)
.output(); .output();
match output { match output {
Ok(result) => { Ok(result) => {
if !result.status.success() { if !result.status.success() {
eprintln!("Error processing compressed file {:?}: {}", path, String::from_utf8_lossy(&result.stderr)); 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);
}
} }
} }
Err(e) => {
eprintln!("Failed to execute unar for {:?}: {}", path, e);
}
} }
} }
} }
@ -45,6 +48,7 @@ fn main() -> io::Result<()> {
} else { } else {
PathBuf::from(".") PathBuf::from(".")
}; };
process_compressed_files(&start_path)?; process_compressed_files(&start_path)?;
Ok(()) Ok(())