use std::fs; use std::io; use zip::{ZipArchive, result::ZipError}; use std::path::Path; pub fn validate_name(name: &str) -> bool{ name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-') } pub async fn extract_archive(archive_path: String, output: String) -> Result<(), String> { println!("starting extraction of {}", archive_path); fs::create_dir(&output).map_err(|e| format!("Failed to create archive output folder, reason : {}", e))?; let mut archive = match fs::File::open(archive_path) .map_err(ZipError::from) .and_then(ZipArchive::new) { Ok(archive) => archive, Err(e) => { return Err(format!("failed to open archive, reason : {:?}", e)); } }; for i in 0..archive.len() { let mut file = match archive.by_index(i) { Ok(file) => file, Err(e) => { return Err(format!("extraction failed, reason : {}", e)); } }; let mut out_path = match file.enclosed_name() { Some(path) => path, None => { return Err("Invalid file path".to_string()); } }; out_path = Path::new(&output).join(out_path); if file.is_dir() { if let Err(e) = fs::create_dir_all(&out_path) { return Err(format!("extraction failed, reason : {}", e)); } } else { if let Some(p) = out_path.parent() && !p.exists() && let Err(e) = fs::create_dir_all(p) { return Err(format!("extraction failed, reason : {}", e)); } let _ = fs::File::create(&out_path) .and_then(|mut outfile| io::copy(&mut file, &mut outfile)) .map_err(|e| format!("extraction failed, reason: {}", e))?; } } println!("extraction done"); Ok(()) } pub fn move_all_elements(src: &Path, dst: &Path) -> io::Result<()> { // 1. Ensure the destination directory exists; if not, create it if !dst.exists() { fs::create_dir_all(dst)?; println!("Created destination directory: {:?}", dst); } // 2. Read the contents of the source directory for entry in fs::read_dir(src)? { let entry = entry?; let file_name = entry.file_name(); // 3. Construct the new path (destination + filename) let old_path = entry.path(); let new_path = dst.join(file_name); // 4. Perform the move // fs::rename works for both files and directories fs::rename(&old_path, &new_path)?; println!("Moved: {:?} -> {:?}", old_path, new_path); } Ok(()) }