I have a Gulp task where I add lots of files (more than 2700 in one case, but it can be several thousands in some others cases) in a ZIP file. The code is as follow:
const fs = require('fs');
const archiver = require('archiver')('zip');
let zip = fs.createWriteStream('my-archive.zip');
return gulp.src('app/**/*')
.pipe(through.obj((file, encoding, cb) => {
let pathInZip = '...';
if (!isADirectory(file.path)) { // Do not zip the directory itself
archiver.append(fs.createReadStream(file.path), {
name: pathInZip,
mode: fs.statSync(file.path)
});
}
cb(null, file);
}, cb => {
// Now create the ZIP file!
archiver.pipe(zip);
archiver.finalize();
cb();
}));
This code works on small projects, but when it deals with more than 2000 files, I get the following error:
events.js:154
throw er; // Unhandled 'error' event
^
Error: EMFILE: too many open files, open 'd:\dev\app\some\file'
at Error (native)
So I understand that having 2000+ files opened at the same time before writing them in the ZIP is not a good idea.
How can I ask the ZIP file to be written without having the need to open all the files?
Thanks.
For information: node 5.5.0 / npm 3.8.5 / archiver 1.0.0 / windows