-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzip.js
40 lines (39 loc) · 1.07 KB
/
unzip.js
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 yauzl = require("yauzl");
const fs = require("fs");
const path = require("path");
module.exports.unzipBuffer = function(buffer, outputPath, callback) {
yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipfile) => {
if (err) {
callback(err);
return;
}
zipfile.readEntry();
zipfile.on("entry", entry => {
const outputFilePath = path.join(outputPath, entry.fileName);
if (/\/$/.test(entry.fileName)) {
// Directory
fs.existsSync(outputFilePath)
? zipfile.readEntry()
: fs.mkdir(
outputFilePath,
err => (err ? callback(err) : zipfile.readEntry())
);
} else {
// File
zipfile.openReadStream(entry, (err, readStream) => {
if (err) {
callback(err);
return;
}
readStream.on("end", () => {
zipfile.readEntry();
});
readStream.pipe(fs.createWriteStream(outputFilePath));
});
}
});
zipfile.on("end", () => {
callback();
});
});
};