From 723a7d54bd4823b2a9876fdea7b8b08cde74f669 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 6 Jan 2014 22:32:27 +0000 Subject: Use same module for node.js and browsers --- browser/unzip.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 browser/unzip.js (limited to 'browser') diff --git a/browser/unzip.js b/browser/unzip.js new file mode 100644 index 0000000..97fcdb1 --- /dev/null +++ b/browser/unzip.js @@ -0,0 +1,32 @@ +var q = require("q"); + +exports.openZip = openZip; + +function openZip(options) { + if (options.arrayBuffer) { + return openArrayBuffer(options.arrayBuffer); + } else { + return q.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 q.when(buffer.toString(encoding)); + } else { + return q.when(buffer); + } + } + return { + exists: exists, + read: read + }; +} -- cgit v1.2.3