From 251ee8b2c5b8c002d9b77d1ae0d1267e0270919f Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 25 Jul 2023 16:50:13 -0700 Subject: [PATCH] unzip function --- src/lib.rs | 36 +++++++++++++++++++++++++++++++++++- src/main.rs | 3 +++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index dd43b97..b261967 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,8 @@ -use std::{fs, path::Path}; +use compress_tools::{uncompress_archive, Ownership}; +use std::{ + fs::{self, File}, + path::Path, +}; use serde::{Deserialize, Serialize}; @@ -69,8 +73,38 @@ pub fn read_config(file: &String) -> Config { } pub fn unzip(cfg: Config) { + if let Some(_) = Path::new(&cfg.archives).parent() { + fs::create_dir_all(&cfg.archives).expect("couldn't create needed folders") + }; + if let Some(_) = Path::new(&cfg.folders).parent() { + fs::create_dir_all(&cfg.folders).expect("couldn't create needed folders") + }; + // get all files in cfg.archives + let all = fs::read_dir(cfg.archives).expect("couldn't read archives directory"); // unzip them all into cfg.folders + for a in all { + let archive = a.expect("quite literally, how"); + let afn = archive.file_name(); + let name = afn.to_str().unwrap(); + let is_valid = name.ends_with(".zip") || name.ends_with(".7z") || name.ends_with(".rar"); + let unzipped = fs::read_dir(&cfg.folders).expect("couldn't read folders directory"); + if fs::metadata(archive.path()).unwrap().is_file() + && is_valid + && !unzipped + .map(|n| n.unwrap().file_name().to_str().unwrap().to_string()) + .collect::>() + .contains(&afn.to_str().unwrap().to_string()) + { + println!("{}", archive.file_name().to_str().unwrap()); + // unzip into cfg.folders + let mut source = File::open(archive.path()).unwrap(); + let fuckyourustidontcareaboutvariablenamesanymore = format!("{}/{name}", cfg.folders); + let dest = Path::new(&fuckyourustidontcareaboutvariablenamesanymore); + + uncompress_archive(&mut source, &dest, Ownership::Preserve).unwrap(); + } + } } pub fn deploy(cfg: Config) { diff --git a/src/main.rs b/src/main.rs index 879ef1b..b8c060c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,4 +23,7 @@ fn main() { println!("Steam: {}", steam_location); cyber_mod_manager::create_config(&format!("{config_location}/CyberModManager/config.json")); + + let cfg = cyber_mod_manager::read_config(&format!("{config_location}/CyberModManager/config.json")); + cyber_mod_manager::unzip(cfg); }