Skip to content

Commit

Permalink
options.mutiple collect al the file that match the rule
Browse files Browse the repository at this point in the history
  • Loading branch information
faluelv committed Sep 27, 2016
1 parent 6c8bc79 commit fc96d1c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
44 changes: 27 additions & 17 deletions zip-browser.js
Expand Up @@ -12,7 +12,7 @@ function Unzip(file/* or blob */) {
this.file = file;
}

Unzip.prototype.destroy = function() {
Unzip.prototype.destroy = function () {
this.file = null;
};

Expand All @@ -21,9 +21,10 @@ Unzip.prototype.destroy = function() {
* @param {Array<String>} whatYouNeed
* @param {Object} options (Optional)
* @param {String} options.type Currently, only support 'blob', by default it will return Buffers
* @param {Boolean} options.multiple If true, it will collect all the file which match the whtaYouNeed rule
* @param callback Will be called like callback(err, buffers)
*/
Unzip.prototype.getBuffer = function(whatYouNeed, options, callback) {
Unzip.prototype.getBuffer = function (whatYouNeed, options, callback) {
if (!utils.isArray(whatYouNeed) || !utils.isFunction(callback)) {
return callback(new Error('getBuffer: invalid param, expect first param to be an Array and the second param to be a callback function'));
}
Expand All @@ -33,48 +34,57 @@ Unzip.prototype.getBuffer = function(whatYouNeed, options, callback) {
options = {};
}

whatYouNeed = whatYouNeed.map(function(rule) {
whatYouNeed = whatYouNeed.map(function (rule) {
if (typeof rule === 'string') {
rule = rule.split('\u0000').join('');
}
return rule;
});

this.getEntries(function(error, entries) {
var isMultiple = options && options.multiple || false;

this.getEntries(function (error, entries) {
if (error) return callback(error);

var matchedEntries = {};

entries.forEach(function(entry) {
entries.forEach(function (entry) {
// Add regexp support
return whatYouNeed.some(function(entryName) {
return whatYouNeed.some(function (entryName) {
if (utils.isThisWhatYouNeed(entryName, entry.filename)) {
matchedEntries[entryName] = entry;
if (isMultiple) {
var obj = { fileName: entryName, buffer: entry };
matchedEntries[entryName]
? matchedEntries[entryName].push(obj)
: (matchedEntries[entryName] = [obj]);
} else {
matchedEntries[entryName] = entry;
}
return true;
}
});
});

iterator(matchedEntries, options, function(error, bufferArray) {
iterator(matchedEntries, options, function (error, bufferArray) {
callback(error, bufferArray, entries.length);
});
});
};

Unzip.prototype.getEntries = function(callback) {
zip.createReader(new zip.BlobReader(this.file), function(zipReader) {
zipReader.getEntries(function(entries) {
Unzip.prototype.getEntries = function (callback) {
zip.createReader(new zip.BlobReader(this.file), function (zipReader) {
zipReader.getEntries(function (entries) {
callback(null, entries, entries.length);
});
});
};

Unzip.getEntryData = function(entry, callback) {
Unzip.getEntryData = function (entry, callback) {
var writerType = 'blob';

var writer = new zip.BlobWriter();

entry.getData(writer, function(blob) {
entry.getData(writer, function (blob) {
callback(null, blob, entry.length);
});
};
Expand All @@ -95,9 +105,9 @@ function iterator(entries, options, callback) {
callback(null, {}, serialize.length);
}

serialize.forEach(function(entryInfo) {
(function(name, entry) {
Unzip.getEntryData(entry, function(err, blob) {
serialize.forEach(function (entryInfo) {
(function (name, entry) {
Unzip.getEntryData(entry, function (err, blob) {
if (err) return callback(err);

if (options.type === 'blob') {
Expand All @@ -106,7 +116,7 @@ function iterator(entries, options, callback) {
callback(null, output, serialize.length);
}
} else {
blobToBuffer(blob, function(error, buffer) {
blobToBuffer(blob, function (error, buffer) {
if (error) {
console.error(error);
return callback(error);
Expand Down
55 changes: 37 additions & 18 deletions zip-node.js
Expand Up @@ -6,7 +6,7 @@ function Unzip(destPath) {
this.path = destPath;
}

Unzip.prototype.destroy = function() {
Unzip.prototype.destroy = function () {
this.path = null;
};

Expand All @@ -16,8 +16,8 @@ Unzip.prototype.destroy = function() {
* @param callback
* @param onEnd
*/
Unzip.prototype.getEntries = function(callback, onEnd) {
yauzl.open(this.path, { lazyEntries: true }, function(err, zipfile) {
Unzip.prototype.getEntries = function (callback, onEnd) {
yauzl.open(this.path, { lazyEntries: true }, function (err, zipfile) {
if (err) {
callback(err);
return;
Expand All @@ -27,11 +27,11 @@ Unzip.prototype.getEntries = function(callback, onEnd) {
zipfile.readEntry();
}

zipfile.on('entry', function(entry) {
zipfile.on('entry', function (entry) {
callback(null, zipfile, entry, next);
});

zipfile.on('end', function() {
zipfile.on('end', function () {
if (utils.isFunction(onEnd)) {
onEnd();
}
Expand All @@ -46,37 +46,56 @@ Unzip.prototype.getEntries = function(callback, onEnd) {
* @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) {
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) {
whatYouNeed = whatYouNeed.map(function (rule) {
if (typeof rule === 'string') {
rule = rule.split('\u0000').join('');
}
return rule;
});

this.getEntries(function(error, zipfile, entry, next) {
this.getEntries(function (error, zipfile, entry, next) {
if (error) return callback(error);
entryCount = zipfile.entryCount;
var findIt = whatYouNeed.some(function(rule) {
currentIndex++;
var findIt = whatYouNeed.some(function (rule) {
if (utils.isThisWhatYouNeed(rule, entry.fileName)) {
Unzip.getEntryData(zipfile, entry, function(error, buffer) {
Unzip.getEntryData(zipfile, entry, function (error, buffer) {
if (error) {
callback(error);
return;
}
output[rule] = buffer;
finishedNumber++;

if (finishedNumber >= whatYouNeed.length) {
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) || (isMutiple && currentIndex >= entryCount)) {
callback(null, output, entryCount);
} else {
next();
Expand All @@ -88,27 +107,27 @@ Unzip.prototype.getBuffer = function(whatYouNeed, options, callback) {

if (!findIt) next();

}, function() {
}, function () {
if (finishedNumber < whatYouNeed.length) {
callback(null, output, entryCount);
}
});
};

Unzip.getEntryData = function(zipfile, entry, callback) {
Unzip.getEntryData = function (zipfile, entry, callback) {
const bufferArr = [];
zipfile.openReadStream(entry, function(err, readStream) {
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
callback(err);
readStream.destroy();
return;
}

readStream.on('data', function(chunk) {
readStream.on('data', function (chunk) {
bufferArr.push(chunk);
});

readStream.on('end', function() {
readStream.on('end', function () {
const buffer = Buffer.concat(bufferArr);
callback(null, buffer);
});
Expand Down

0 comments on commit fc96d1c

Please sign in to comment.