fix file starts with minus.

This commit is contained in:
licsber 2024-09-26 17:09:16 +08:00
parent a8e45940ea
commit 81bcb8f543
4 changed files with 53 additions and 40 deletions

2
Cargo.lock generated
View File

@ -4,4 +4,4 @@ version = 3
[[package]]
name = "unzip-all"
version = "0.1.0"
version = "0.2.0"

View File

@ -1,6 +1,6 @@
[package]
name = "unzip-all"
version = "0.1.0"
version = "0.2.0"
authors = ["licsber <admin@licsber.site>"]
edition = "2021"

View File

@ -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<PathBuf> = 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<String> = 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)
}

35
src/unar.rs Normal file
View File

@ -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(())
}