From 2614ad091992fc23a5c0767256053f5bf445f74b Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Fri, 25 Oct 2024 18:17:48 +0200 Subject: [PATCH] Zip extraction implementation --- app/main.ts | 44 ++++++++++++++++++++++++++++++++++++++++++- app/package-lock.json | 38 +++++++++++++++++++++++++++++++++++-- app/package.json | 2 ++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/app/main.ts b/app/main.ts index 586f416..bf86c37 100644 --- a/app/main.ts +++ b/app/main.ts @@ -10,6 +10,7 @@ import * as tar from 'tar'; import * as os from 'os'; import * as pidusage from 'pidusage'; import AutoLaunch from './auto-launch'; +import AdmZip from 'adm-zip'; interface Stats { /** @@ -529,6 +530,47 @@ const extractTarBz2 = (filePath: string, destination: string): Promise = }); }; +const extractZip = (filePath: string, destination: string): Promise => { + return new Promise((resolve, reject) => { + try { + const zip = new AdmZip(filePath); + + // Ensure destination exists + if (!fs.existsSync(destination)) { + fs.mkdirSync(destination, { recursive: true }); + } + + // Extract the ZIP file + zip.extractAllTo(destination, true); + + // Get the name of the extracted folder + const extractedEntries = zip.getEntries(); + const folderName = extractedEntries[0]?.entryName.split('/')[0]; + + // Ensure folder name exists + if (!folderName) { + reject(new Error("Could not determine the extracted folder name")); + return; + } + + resolve(path.join(destination, folderName)); + } catch (error) { + reject(error); + } + }); +}; + +const extract = (filePath: string, destination: string): Promise => { + if (filePath.endsWith('.zip')) { + return extractZip(filePath, destination); + } + else if (filePath.endsWith('.tar.bz2')) { + return extractTarBz2(filePath, destination); + } + + throw new Error("Unknown file type " + filePath); +} + // #endregion function showNotification(options?: NotificationConstructorOptions): void { @@ -637,7 +679,7 @@ try { // Estrai il file const fPath = `${destination}/${fileName}`; event.sender.send('download-progress', { progress: 100, status: 'Extracting' }); - const extractedDir = await extractTarBz2(fPath, destination); + const extractedDir = await extract(fPath, destination); event.sender.send('download-progress', { progress: 100, status: 'Download and extraction completed successfully' }); event.sender.send('download-progress', { progress: 200, status: `${destination}/${extractedDir}` }); diff --git a/app/package-lock.json b/app/package-lock.json index bbc6eac..67f5775 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -1,13 +1,14 @@ { "name": "monerod-gui", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "monerod-gui", - "version": "0.1.0", + "version": "0.1.1", "dependencies": { + "adm-zip": "^0.5.16", "applescript": "^1.0.0", "os": "^0.1.2", "pidusage": "^3.0.2", @@ -16,6 +17,7 @@ "winreg": "^1.2.5" }, "devDependencies": { + "@types/adm-zip": "^0.5.5", "@types/auto-launch": "^5.0.5", "@types/pidusage": "^2.0.5", "@types/winreg": "^1.2.36" @@ -57,12 +59,30 @@ "node": ">=14" } }, + "node_modules/@types/adm-zip": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/@types/adm-zip/-/adm-zip-0.5.5.tgz", + "integrity": "sha512-YCGstVMjc4LTY5uK9/obvxBya93axZOVOyf2GSUulADzmLhYE45u2nAssCs/fWBs1Ifq5Vat75JTPwd5XZoPJw==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/auto-launch": { "version": "5.0.5", "resolved": "https://registry.npmjs.org/@types/auto-launch/-/auto-launch-5.0.5.tgz", "integrity": "sha512-/nGvQZSzM/pvCMCh4Gt2kIeiUmOP/cKGJbjlInI+A+5MoV/7XmT56DJ6EU8bqc3+ItxEe4UC2GVspmPzcCc8cg==", "dev": true }, + "node_modules/@types/node": { + "version": "22.8.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.8.0.tgz", + "integrity": "sha512-84rafSBHC/z1i1E3p0cJwKA+CfYDNSXX9WSZBRopjIzLET8oNt6ht2tei4C7izwDeEiLLfdeSVBv1egOH916hg==", + "dev": true, + "dependencies": { + "undici-types": "~6.19.8" + } + }, "node_modules/@types/pidusage": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/@types/pidusage/-/pidusage-2.0.5.tgz", @@ -75,6 +95,14 @@ "integrity": "sha512-DtafHy5A8hbaosXrbr7YdjQZaqVewXmiasRS5J4tYMzt3s1gkh40ixpxgVFfKiQ0JIYetTJABat47v9cpr/sQg==", "dev": true }, + "node_modules/adm-zip": { + "version": "0.5.16", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.16.tgz", + "integrity": "sha512-TGw5yVi4saajsSEgz25grObGHEUaDrniwvA2qwSC060KfqGPdglhvPMA2lPIoxs3PQIItj2iag35fONcQqgUaQ==", + "engines": { + "node": ">=12.0" + } + }, "node_modules/ansi-regex": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.1.0.tgz", @@ -562,6 +590,12 @@ "through": "^2.3.8" } }, + "node_modules/undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", + "dev": true + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/app/package.json b/app/package.json index 8cbbadd..dd8c373 100644 --- a/app/package.json +++ b/app/package.json @@ -10,6 +10,7 @@ "main": "main.js", "private": true, "dependencies": { + "adm-zip": "^0.5.16", "applescript": "^1.0.0", "os": "^0.1.2", "pidusage": "^3.0.2", @@ -18,6 +19,7 @@ "winreg": "^1.2.5" }, "devDependencies": { + "@types/adm-zip": "^0.5.5", "@types/auto-launch": "^5.0.5", "@types/pidusage": "^2.0.5", "@types/winreg": "^1.2.36"