aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-05-07 10:41:44 +0100
committerMichael Williamson <mike@zwobble.org>2014-05-07 10:41:44 +0100
commit0c11e5675f9a0d4e263afed413aa656c20734f77 (patch)
tree8132dbe91dc0bfeb9ed0ea49655cd40e39790de9 /lib
parent17b1f33095c13e02bdfa5ffabd378a34d3aa0751 (diff)
Use jszip on server-side as well as browser-side
Diffstat (limited to 'lib')
-rw-r--r--lib/promises.js1
-rw-r--r--lib/unzip.js69
-rw-r--r--lib/zipfile.js27
3 files changed, 36 insertions, 61 deletions
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
+ };
+}
+