ArchiveAds's picture
Create zip.js
5b84203 verified
Raw
History Blame Contribute Delete
1.17 kB
const fs = require('fs');
const archiver = require('archiver');
const path = require('path');
function createZip(files, zipPath) {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(zipPath);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
resolve(zipPath);
});
output.on('end', function() {
console.log('Data has been drained');
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn(err);
} else {
reject(err);
}
});
archive.on('error', function(err) {
reject(err);
});
archive.pipe(output);
files.forEach(file => {
if (fs.existsSync(file.path)) {
archive.file(file.path, { name: file.name });
} else {
console.warn(`File not found: ${file.path}`);
}
});
archive.finalize();
});
}
module.exports = { createZip };