aboutsummaryrefslogtreecommitdiff
path: root/lib/unzip.js
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/unzip.js
parent17b1f33095c13e02bdfa5ffabd378a34d3aa0751 (diff)
Use jszip on server-side as well as browser-side
Diffstat (limited to 'lib/unzip.js')
-rw-r--r--lib/unzip.js69
1 files changed, 8 insertions, 61 deletions
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;
-}