solution for loose file mods

This commit is contained in:
SpikeHD
2022-07-24 20:59:08 -07:00
parent 412acdd317
commit 0551f3e6a0
3 changed files with 39 additions and 4 deletions

View File

@@ -3,7 +3,13 @@ use std::path;
use std::thread; use std::thread;
#[tauri::command] #[tauri::command]
pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level: Option<bool>) { pub fn unzip(
window: tauri::Window,
zipfile: String,
destpath: String,
top_level: Option<bool>,
folder_if_loose: Option<bool>,
) {
// Read file TODO: replace test file // Read file TODO: replace test file
let f = match File::open(&zipfile) { let f = match File::open(&zipfile) {
Ok(f) => f, Ok(f) => f,
@@ -27,10 +33,33 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String, top_level
// Run extraction in seperate thread // Run extraction in seperate thread
thread::spawn(move || { thread::spawn(move || {
let full_path = &write_path; let mut full_path = write_path.clone();
window.emit("extract_start", &zipfile).unwrap(); 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)) { match zip_extract::extract(&f, &full_path, top_level.unwrap_or(false)) {
Ok(_) => { Ok(_) => {
println!( println!(

View File

@@ -65,7 +65,7 @@ export class Mods extends React.Component<IProps, IState> {
const firstLink = dlLinks[0].downloadUrl const firstLink = dlLinks[0].downloadUrl
this.props.downloadHandler.addDownload(firstLink, modPath, async () => { 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 // Write a modinfo.json file
invoke('write_file', { invoke('write_file', {

View File

@@ -6,12 +6,18 @@ interface UnzipPayload {
new_folder: string new_folder: string
} }
export function unzip(file: string, dest: string, topLevelStrip?: boolean): Promise<UnzipPayload> { export function unzip(
file: string,
dest: string,
topLevelStrip?: boolean,
folderIfLoose?: boolean
): Promise<UnzipPayload> {
return new Promise((resolve) => { return new Promise((resolve) => {
invoke('unzip', { invoke('unzip', {
zipfile: file, zipfile: file,
destpath: dest, destpath: dest,
topLevelStrip, topLevelStrip,
folderIfLoose,
}) })
listen('extract_end', ({ payload }) => { listen('extract_end', ({ payload }) => {