-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
173 lines (160 loc) · 5.67 KB
/
main.js
File metadata and controls
173 lines (160 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const yauzl = require('yauzl');
let mainWindow = null;
let progressWindow = null;
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 720,
height: 640,
minWidth: 560,
minHeight: 520,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
title: 'Simple Unzipper — Anakatech',
backgroundColor: '#0f1419',
show: false,
});
mainWindow.loadFile(path.join(__dirname, 'src', 'renderer', 'index.html'));
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
mainWindow.on('closed', () => { mainWindow = null; });
}
function createProgressWindow(parent) {
if (progressWindow) {
progressWindow.focus();
return progressWindow;
}
progressWindow = new BrowserWindow({
width: 480,
height: 320,
parent,
modal: true,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
backgroundColor: '#0f1419',
show: false,
});
progressWindow.loadFile(path.join(__dirname, 'src', 'renderer', 'progress.html'));
progressWindow.once('ready-to-show', () => progressWindow.show());
progressWindow.on('closed', () => {
progressWindow = null;
if (mainWindow && !mainWindow.isDestroyed()) mainWindow.focus();
});
return progressWindow;
}
// Normalize zip entry path to use OS separators (handles / and \)
function normalizeEntryPath(fileName) {
return fileName.split(/[/\\]/).filter(Boolean).join(path.sep);
}
// MS-DOS directory attribute bit in zip external file attributes
const FILE_ATTR_DIR = 0x10;
function extractZip(zipPath, outDir) {
return new Promise((resolve, reject) => {
yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
if (err) return reject(err);
zipfile.readEntry();
zipfile.on('entry', (entry) => {
const rawName = entry.fileName;
const normalizedName = normalizeEntryPath(rawName.replace(/[/\\]+$/, ''));
if (!normalizedName) {
zipfile.readEntry();
return;
}
const destPath = path.join(outDir, normalizedName);
const hasTrailingSlash = /[/\\]$/.test(rawName);
const isDirByAttr = entry.uncompressedSize === 0 && (entry.externalFileAttributes & FILE_ATTR_DIR) === FILE_ATTR_DIR;
const isDirectory = hasTrailingSlash || isDirByAttr;
if (isDirectory || (entry.uncompressedSize === 0 && !path.extname(normalizedName))) {
fs.mkdirSync(destPath, { recursive: true });
zipfile.readEntry();
return;
}
fs.mkdirSync(path.dirname(destPath), { recursive: true });
zipfile.openReadStream(entry, (e, readStream) => {
if (e) {
zipfile.readEntry();
return;
}
const writeStream = fs.createWriteStream(destPath);
readStream.pipe(writeStream);
writeStream.on('finish', () => {
writeStream.close();
zipfile.readEntry();
});
});
});
zipfile.on('end', () => resolve());
zipfile.on('error', reject);
});
});
}
async function unzipAll(sourceDir, destDir) {
const names = fs.readdirSync(sourceDir);
const zipFiles = names.filter((n) => /\.(zip|ZIP)$/.test(path.extname(n)));
const results = { total: zipFiles.length, done: 0, errors: [] };
if (zipFiles.length === 0) {
if (progressWindow && !progressWindow.isDestroyed()) {
progressWindow.webContents.send('unzip-complete', results);
}
return results;
}
for (const name of zipFiles) {
const zipPath = path.join(sourceDir, name);
const folderName = path.basename(name, path.extname(name));
const outDirForZip = path.join(destDir, folderName);
try {
await extractZip(zipPath, outDirForZip);
results.done++;
if (progressWindow && !progressWindow.isDestroyed()) {
progressWindow.webContents.send('unzip-progress', results);
}
} catch (e) {
results.errors.push({ file: name, message: e.message });
}
}
return results;
}
ipcMain.handle('select-dir', async (event, which) => {
const win = BrowserWindow.getFocusedWindow() || BrowserWindow.fromWebContents(event.sender);
const result = await dialog.showOpenDialog(win || mainWindow, {
properties: ['openDirectory'],
title: which === 'source' ? 'Select folder containing ZIP files' : 'Select folder for unzipped files',
});
return result.canceled ? null : result.filePaths[0];
});
ipcMain.handle('start-unzip', async (_, sourceDir, destDir) => {
const win = BrowserWindow.getFocusedWindow();
if (!sourceDir || !destDir) throw new Error('Please select both source and destination folders.');
if (!fs.existsSync(sourceDir)) throw new Error('Source folder does not exist.');
fs.mkdirSync(destDir, { recursive: true });
createProgressWindow(win);
progressWindow.webContents.once('did-finish-load', async () => {
try {
const results = await unzipAll(sourceDir, destDir);
if (progressWindow && !progressWindow.isDestroyed()) {
progressWindow.webContents.send('unzip-complete', results);
}
} catch (e) {
if (progressWindow && !progressWindow.isDestroyed()) {
progressWindow.webContents.send('unzip-error', e.message);
}
}
});
return { ok: true };
});
ipcMain.handle('close-progress', () => {
if (progressWindow && !progressWindow.isDestroyed()) progressWindow.close();
});
app.whenReady().then(createMainWindow);
app.on('window-all-closed', () => app.quit());