unzip function

This commit is contained in:
SadlyNotSappho 2023-07-25 16:50:13 -07:00
parent 795462120d
commit 251ee8b2c5
2 changed files with 38 additions and 1 deletions

View File

@ -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}; use serde::{Deserialize, Serialize};
@ -69,8 +73,38 @@ pub fn read_config(file: &String) -> Config {
} }
pub fn unzip(cfg: 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 // 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 // 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::<Vec<String>>()
.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) { pub fn deploy(cfg: Config) {

View File

@ -23,4 +23,7 @@ fn main() {
println!("Steam: {}", steam_location); println!("Steam: {}", steam_location);
cyber_mod_manager::create_config(&format!("{config_location}/CyberModManager/config.json")); 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);
} }