extract unar method to independent fn.

This commit is contained in:
licsber 2024-09-08 10:38:50 +08:00
parent a99523f4e6
commit 99c96a0b41

View File

@ -4,6 +4,31 @@ use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
use std::env; use std::env;
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(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);
return Err(e);
}
}
Ok(())
}
fn process_compressed_files(path: &Path) -> io::Result<()> { fn process_compressed_files(path: &Path) -> io::Result<()> {
if !path.is_dir() { if !path.is_dir() {
return Err(io::Error::new(io::ErrorKind::Other, "Not a directory")); return Err(io::Error::new(io::ErrorKind::Other, "Not a directory"));
@ -16,25 +41,7 @@ fn process_compressed_files(path: &Path) -> io::Result<()> {
process_compressed_files(&entry_path)?; process_compressed_files(&entry_path)?;
} else if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) { } else if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) {
if ext == "rar" || ext == "zip" { if ext == "rar" || ext == "zip" {
println!("UNAR: {}", entry_path.display()); return unar_file(&entry_path);
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);
}
}
} }
} }
} }