forked from xiaoyuze88/isomorphic-unzip
-
Notifications
You must be signed in to change notification settings - Fork 5
/
zip-node.js
137 lines (118 loc) · 3.26 KB
/
zip-node.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var yauzl = require('yauzl');
var utils = require('./lib/utils');
function Unzip(destPath) {
if (!(this instanceof Unzip)) return new Unzip(path);
this.path = destPath;
}
Unzip.prototype.destroy = function () {
this.path = null;
};
/**
* Iterator all entries,
*
* @param callback
* @param onEnd
*/
Unzip.prototype.getEntries = function (callback, onEnd) {
yauzl.open(this.path, { lazyEntries: true }, function (err, zipfile) {
if (err) {
callback(err);
return;
}
function next() {
zipfile.readEntry();
}
zipfile.on('entry', function (entry) {
callback(null, zipfile, entry, next);
});
zipfile.on('end', function () {
if (utils.isFunction(onEnd)) {
onEnd();
}
});
next();
});
};
/**
*
* @param {Array<String>} whatYouNeed
* @param {Object} options In node, we don't support any options now.
* @param callback Will be called like callback(err, buffers)
*
* @param options.multiple {boolean} if this param is true, getBuffer will return all the file which match the whatYouNeed reg in an array, each item would be an Object with two props, fileName and buffer
*/
Unzip.prototype.getBuffer = function (whatYouNeed, options, callback) {
var finishedNumber = 0;
const output = {};
var entryCount = 0;
var isMutiple = options && options.multiple || false;
var currentIndex = 0;
if (utils.isFunction(options)) {
callback = options;
}
whatYouNeed = whatYouNeed.map(function (rule) {
if (typeof rule === 'string') {
rule = rule.split('\u0000').join('');
}
return rule;
});
this.getEntries(function (error, zipfile, entry, next) {
if (error) return callback(error);
entryCount = zipfile.entryCount;
currentIndex++;
var findIt = whatYouNeed.some(function (rule) {
if (utils.isThisWhatYouNeed(rule, entry.fileName)) {
Unzip.getEntryData(zipfile, entry, function (error, buffer) {
if (error) {
callback(error);
return;
}
if (isMutiple) {
var obj = {
fileName: entry.fileName,
buffer: buffer
};
if (output[rule]) {
output[rule].push(obj);
} else {
output[rule] = [obj];
finishedNumber++;
}
} else {
output[rule] = buffer;
finishedNumber++;
}
if (finishedNumber >= whatYouNeed.length && !isMutiple) {
callback(null, output, entryCount);
} else {
next();
}
});
return true;
}
});
if (!findIt) next();
}, function () {
if ((finishedNumber < whatYouNeed.length) || (isMutiple && currentIndex >= entryCount)) {
callback(null, output, entryCount);
}
});
};
Unzip.getEntryData = function (zipfile, entry, callback) {
const bufferArr = [];
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
callback(err);
readStream.destroy();
return;
}
readStream.on('data', function (chunk) {
bufferArr.push(chunk);
});
readStream.on('end', function () {
const buffer = Buffer.concat(bufferArr);
callback(null, buffer);
});
});
};
module.exports = Unzip;