diff --git a/src-tauri/src/unzip.rs b/src-tauri/src/unzip.rs index cfa1810..1d51f10 100644 --- a/src-tauri/src/unzip.rs +++ b/src-tauri/src/unzip.rs @@ -3,7 +3,13 @@ use std::path; use std::thread; #[tauri::command] -pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level: Option) { +pub fn unzip( + window: tauri::Window, + zipfile: String, + destpath: String, + top_level: Option, + folder_if_loose: Option, +) { // Read file TODO: replace test file let f = match File::open(&zipfile) { Ok(f) => f, @@ -27,10 +33,33 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level // Run extraction in seperate thread thread::spawn(move || { - let full_path = &write_path; + let mut full_path = write_path.clone(); window.emit("extract_start", &zipfile).unwrap(); + if folder_if_loose.unwrap_or(false) { + // Create a new folder with the same name as the zip file + let mut file_name = path::Path::new(&zipfile) + .file_name() + .unwrap() + .to_str() + .unwrap(); + + // remove ".zip" from the end of the file name + file_name = &file_name[..file_name.len() - 4]; + + let new_path = full_path.join(file_name); + match std::fs::create_dir_all(&new_path) { + Ok(_) => {} + Err(e) => { + println!("Failed to create directory: {}", e); + return; + } + }; + + full_path = new_path.clone(); + } + match zip_extract::extract(&f, &full_path, top_level.unwrap_or(false)) { Ok(_) => { println!( diff --git a/src/ui/Mods.tsx b/src/ui/Mods.tsx index 1ae42e3..f7c45d6 100644 --- a/src/ui/Mods.tsx +++ b/src/ui/Mods.tsx @@ -65,7 +65,7 @@ export class Mods extends React.Component { const firstLink = dlLinks[0].downloadUrl this.props.downloadHandler.addDownload(firstLink, modPath, async () => { - const unzipRes = await unzip(modPath, modFolder, false) + const unzipRes = await unzip(modPath, modFolder, false, true) // Write a modinfo.json file invoke('write_file', { diff --git a/src/utils/zipUtils.ts b/src/utils/zipUtils.ts index ffd9d59..1b90554 100644 --- a/src/utils/zipUtils.ts +++ b/src/utils/zipUtils.ts @@ -6,12 +6,18 @@ interface UnzipPayload { new_folder: string } -export function unzip(file: string, dest: string, topLevelStrip?: boolean): Promise { +export function unzip( + file: string, + dest: string, + topLevelStrip?: boolean, + folderIfLoose?: boolean +): Promise { return new Promise((resolve) => { invoke('unzip', { zipfile: file, destpath: dest, topLevelStrip, + folderIfLoose, }) listen('extract_end', ({ payload }) => {