Skip to content

Commit

Permalink
Zip extraction implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Oct 25, 2024
1 parent 32530c6 commit 2614ad0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
44 changes: 43 additions & 1 deletion app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -529,6 +530,47 @@ const extractTarBz2 = (filePath: string, destination: string): Promise<string> =
});
};

const extractZip = (filePath: string, destination: string): Promise<string> => {
return new Promise<string>((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<string> => {
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 {
Expand Down Expand Up @@ -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}` });
Expand Down
38 changes: 36 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit 2614ad0

Please sign in to comment.