Skip to content

Commit

Permalink
SDA-4447 Migrate from archiverjs to adm-zip (finos#2089)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenmoussati authored Feb 5, 2024
1 parent 41c68f7 commit b172cc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 15 additions & 22 deletions src/app/reports-handler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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', () => {
Expand All @@ -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;
Expand All @@ -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);
});
};

Expand Down

0 comments on commit b172cc1

Please sign in to comment.