From fc96d1c79963dfab99ce80f3904f0a9c4f5b642b Mon Sep 17 00:00:00 2001 From: faluelv Date: Tue, 27 Sep 2016 11:34:08 +0800 Subject: [PATCH] options.mutiple collect al the file that match the rule --- zip-browser.js | 44 ++++++++++++++++++++++++---------------- zip-node.js | 55 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/zip-browser.js b/zip-browser.js index 0a9c7c7..d92f23e 100644 --- a/zip-browser.js +++ b/zip-browser.js @@ -12,7 +12,7 @@ function Unzip(file/* or blob */) { this.file = file; } -Unzip.prototype.destroy = function() { +Unzip.prototype.destroy = function () { this.file = null; }; @@ -21,9 +21,10 @@ Unzip.prototype.destroy = function() { * @param {Array} 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')); } @@ -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); }); }; @@ -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') { @@ -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); diff --git a/zip-node.js b/zip-node.js index 75bc1ef..e775073 100644 --- a/zip-node.js +++ b/zip-node.js @@ -6,7 +6,7 @@ function Unzip(destPath) { this.path = destPath; } -Unzip.prototype.destroy = function() { +Unzip.prototype.destroy = function () { this.path = null; }; @@ -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; @@ -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(); } @@ -46,37 +46,56 @@ Unzip.prototype.getEntries = function(callback, onEnd) { * @param {Array} 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(); @@ -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); });