I'm using the following code to unzip file which is working OK. I'm getting file with the request (express) and unzip it in specified folder
_unzip: function (req) {
return new Promise((resolve, reject) =>{
var noOfFiles = 0;
let filePath = path.join(req.file.destination, req.file.filename);
logger.info("Unzip filePath: " + filePath);
if (req.file) {
yauzl.open(filePath, function (err, zipFile) {
if (err) throw err;
zipFile.on('entry', (entry) =>{
//console.log(entry.fileName);
if (/\/$/.test(entry.fileName)) {
return;
}
zipFile.openReadStream(entry, (err, readStream) => {
if (err) {
logger.info(err);
reject(err);
} else {
// ensure parent directory exists, and then:
let destinationFileName = "./" + entry.fileName;
let directoryName = path.dirname(destinationFileName);
if (!fs.existsSync(directoryName)) {
mkdirp(directoryName, (err) =>{
if (err) {
logger.info(err);
reject(err);
} else {
writeIntoFile(readStream, destinationFileName, reject);
noOfFiles++;
}
});
} else {
writeIntoFile(readStream, destinationFileName, reject);
noOfFiles++;
}
}
});
}).once('error', (err) =>{
logger.info(err);
reject(err);
}).once('close', () =>{
logger.info("Unpacked " + noOfFiles + " files");
resolve();
});
});
}
});
}