-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (32 loc) · 1.05 KB
/
Copy pathindex.js
File metadata and controls
40 lines (32 loc) · 1.05 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
const fs = require('fs');
const archiver = require('archiver');
/**
* Zips a folder and writes it to a specified destination.
* @param {string} srcFolder - The source folder to zip.
* @param {string} zipFilePath - The path to save the zip file.
* @param {function} callback - Callback function to handle success or error.
*/
function zipFolder(srcFolder, zipFilePath, callback) {
const output = fs.createWriteStream(zipFilePath);
const zipArchive = archiver('zip', {
zlib: { level: 9 }, // Maximum compression level
});
output.on('close', () => {
callback(null);
});
output.on('error', (err) => {
console.error('Error writing archive:', err);
callback(err);
});
zipArchive.on('error', (err) => {
console.error('Error during archiving:', err);
callback(err);
});
zipArchive.pipe(output);
// Add the source folder to the archive
zipArchive.directory(srcFolder, false);
zipArchive.finalize().catch((err) => {
callback(err);
});
}
module.exports = zipFolder;