diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-05-07 10:41:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-05-07 10:41:44 +0100 |
| commit | 0c11e5675f9a0d4e263afed413aa656c20734f77 (patch) | |
| tree | 8132dbe91dc0bfeb9ed0ea49655cd40e39790de9 | |
| parent | 17b1f33095c13e02bdfa5ffabd378a34d3aa0751 (diff) | |
Use jszip on server-side as well as browser-side
| -rw-r--r-- | browser/unzip.js | 26 | ||||
| -rw-r--r-- | lib/promises.js | 1 | ||||
| -rw-r--r-- | lib/unzip.js | 69 | ||||
| -rw-r--r-- | lib/zipfile.js | 27 | ||||
| -rw-r--r-- | package.json | 1 |
5 files changed, 38 insertions, 86 deletions
diff --git a/browser/unzip.js b/browser/unzip.js index 2395fb3..e7ae8b6 100644 --- a/browser/unzip.js +++ b/browser/unzip.js @@ -1,34 +1,12 @@ -var JSZip = require("jszip"); - var promises = require("../lib/promises"); +var zipfile = require("../lib/zipfile"); exports.openZip = openZip; function openZip(options) { if (options.arrayBuffer) { - return openArrayBuffer(options.arrayBuffer); + return zipfile.openArrayBuffer(options.arrayBuffer); } else { return promises.reject(new Error("Could not find file in options")); } } - -function openArrayBuffer(arrayBuffer) { - var zipFile = new JSZip(arrayBuffer); - function exists(name) { - return zipFile.file(name) !== null; - } - - function read(name, encoding) { - var array = zipFile.file(name).asUint8Array(); - var buffer = new Buffer(array); - if (encoding) { - return promises.when(buffer.toString(encoding)); - } else { - return promises.when(buffer); - } - } - return { - exists: exists, - read: read - }; -} diff --git a/lib/promises.js b/lib/promises.js index 32371c1..6f4a6bd 100644 --- a/lib/promises.js +++ b/lib/promises.js @@ -6,6 +6,7 @@ exports.when = bluebird.resolve; exports.resolve = bluebird.resolve; exports.all = bluebird.all; exports.reject = bluebird.reject; +exports.promisify = bluebird.promisify; exports.nfcall = function(func) { var args = Array.prototype.slice.call(arguments, 1); diff --git a/lib/unzip.js b/lib/unzip.js index 4161741..97db80f 100644 --- a/lib/unzip.js +++ b/lib/unzip.js @@ -1,73 +1,20 @@ exports.openZip = openZip; -var promises = require("./promises"); var fs = require("fs"); -var unzip = { - Parse: require("unzip/lib/parse") -}; -var Buffers = require("buffers"); + +var promises = require("./promises"); +var zipfile = require("./zipfile"); + +exports.openZip = openZip; + +var readFile = promises.promisify(fs.readFile); function openZip(options) { if (options.path) { - return unzipStream(fs.createReadStream(options.path)); + return readFile(options.path).then(zipfile.openArrayBuffer); } else if (options.file) { return options.file; } else { return promises.reject(new Error("Could not find file in options")); } } - -function unzipStream(stream) { - var deferred = promises.defer(); - var names = []; - var files = {}; - - stream - .pipe(unzip.Parse()) - .on("entry", function(entry) { - names.push(entry.path); - files[entry.path] = readEntry(entry); - }) - .on("close", function() { - deferred.resolve(zipFile); - }) - .on("error", function(err) { - deferred.reject(err); - }); - - var exists = function(path) { - return names.indexOf(path) !== -1; - }; - - function readEntry(entry) { - var readDeferred = promises.defer(); - var buffers = new Buffers(); - - entry - .on("data", function(data) { - buffers.push(data); - }) - .on("end", function() { - readDeferred.resolve(buffers); - }) - .on("error", readDeferred.reject.bind(readDeferred)); - - return readDeferred.promise; - } - var zipFile = { - read: function(path, encoding) { - if (!exists(path)) { - return promises.reject(new Error("No file in zip: " + path)); - } - return files[path].then(function(buffer) { - if (encoding) { - return buffer.toString(encoding); - } else { - return buffer.toBuffer(); - } - }); - }, - exists: exists - }; - return deferred.promise; -} diff --git a/lib/zipfile.js b/lib/zipfile.js new file mode 100644 index 0000000..2c004b7 --- /dev/null +++ b/lib/zipfile.js @@ -0,0 +1,27 @@ +var JSZip = require("jszip"); + +var promises = require("./promises"); + +exports.openArrayBuffer = openArrayBuffer; + +function openArrayBuffer(arrayBuffer) { + var zipFile = new JSZip(arrayBuffer); + function exists(name) { + return zipFile.file(name) !== null; + } + + function read(name, encoding) { + var array = zipFile.file(name).asUint8Array(); + var buffer = new Buffer(array); + if (encoding) { + return promises.when(buffer.toString(encoding)); + } else { + return promises.when(buffer); + } + } + return { + exists: exists, + read: read + }; +} + diff --git a/package.json b/package.json index fcd8cca..786bb60 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "sax": "~0.5.2", "underscore": "~1.4.4", "lop": "~0.2.8", - "unzip": "~0.1.7", "async": "~0.2.6", "buffers": "~0.1.1", "nomnom": "~1.6.2", |
