SDA-4447 Migrate from archiverjs to adm-zip (#2089)

This commit is contained in:
Salah Benmoussati 2024-02-05 09:41:28 +01:00 committed by GitHub
parent 41c68f77d2
commit b172cc15aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 23 deletions

View File

@ -212,7 +212,7 @@
},
"dependencies": {
"@types/lazy-brush": "^1.0.0",
"archiver": "5.3.1",
"adm-zip": "^0.5.10",
"async.map": "0.5.2",
"classnames": "2.2.6",
"electron-dl": "3.5.0",

View File

@ -1,4 +1,4 @@
import * as archiver from 'archiver';
import * as zip from 'adm-zip';
import { app, BrowserWindow, dialog, shell, WebContents } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
@ -24,10 +24,10 @@ const generateArchiveForDirectory = (
fileExtensions: string[],
retrievedLogs: ILogs[],
): Promise<void> => {
return new Promise((resolve, reject) => {
return new Promise((resolve, _reject) => {
logger.info(`reports-handler: generating archive for directory ${source}`);
const output = fs.createWriteStream(destination);
const archive = archiver('zip', { zlib: { level: 9 } });
const archive = new zip();
const filesForCleanup: string[] = [];
output.on('close', () => {
@ -40,31 +40,17 @@ const generateArchiveForDirectory = (
return resolve();
});
archive.on('error', (err: Error) => {
for (const file of filesForCleanup) {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
}
logger.error(
`reports-handler: error archiving directory for ${source} with error ${err}`,
);
return reject(err);
});
archive.pipe(output);
const files = fs.readdirSync(source);
files
.filter((file) => fileExtensions.indexOf(path.extname(file)) !== -1)
.forEach((file) => {
switch (path.extname(file)) {
case '.log':
archive.file(source + '/' + file, { name: 'logs/' + file });
archive.addLocalFile(source + '/' + file, 'logs');
break;
case '.dmp':
case '.txt': // on Windows .txt files will be created as part of crash dump
archive.file(source + '/' + file, { name: 'crashes/' + file });
archive.addLocalFile(source + '/' + file, 'crashes');
break;
default:
break;
@ -74,12 +60,19 @@ const generateArchiveForDirectory = (
for (const logs of retrievedLogs) {
for (const logFile of logs.logFiles) {
const file = path.join(source, logFile.filename);
archive.file(file, { name: 'logs/' + logFile.filename });
archive.addLocalFile(file, 'logs');
filesForCleanup.push(file);
}
}
archive.finalize();
archive
.writeZipPromise(destination)
.then(() => {
logger.info('reports-handler: successfully created archive');
})
.catch((error) => {
logger.error('reports-handler: error while archiving ', error);
});
archive.writeZip(destination);
});
};