diff --git a/lib/zip.js/zip.js b/lib/zip.js/zip.js index 18bd748..a79fbc7 100644 --- a/lib/zip.js/zip.js +++ b/lib/zip.js/zip.js @@ -136,7 +136,7 @@ define(function(require) { } function getData(callback) { - callback(that.data); + callback(); // XXX I don't think I need getData to return anything... that.data); } function writeUint8Array(array, callback, onerror) { diff --git a/src/shell/shell.js b/src/shell/shell.js index 0dd50b7..c1d9a66 100644 --- a/src/shell/shell.js +++ b/src/shell/shell.js @@ -514,6 +514,59 @@ define(function(require) { }, callback); }; + Shell.prototype.zip = function(zipfile, paths, options, callback) { + var fs = this.fs; + if(typeof options === 'function') { + callback = options; + options = {}; + } + options = options || {}; + callback = callback || function(){}; + + if(!zipfile) { + callback(new Errors.EINVAL('missing zipfile argument')); + return; + } + if(!paths) { + callback(new Errors.EINVAL('missing paths argument')); + return; + } + if(typeof paths === 'string') { + paths = [ paths ]; + } + zipfile = Path.resolve(this.cwd, zipfile); + + var Zip = require('zip.js/zip'); + Zip.createWriter(new Zip.FileWriter(zipfile, fs), function(writer) { + + function add(path, callback) { + var realpath = Path.resolve(this.cwd, path); + fs.stat(realpath, function(err, stats) { + if(err) { + callback(err); + return; + } + + // Tack on zip entry options we care about. + var options = { lastModDate: new Date(stats.mtime) }; + if(stats.isDirectory()) { + options.directory = true; + } + + writer.add(path, new Zip.FileReader(realpath, fs), + function onend() { callback(); }, null, options); + }); + } + + async.eachSeries(paths, add, function(error) { + if(error) return callback(error); + writer.close(function() { + callback(); + }); + }); + }, callback); + }; + return Shell; }); diff --git a/tests/spec/shell/unzip.spec.js b/tests/spec/shell/unzip.spec.js index 331a865..863c276 100644 --- a/tests/spec/shell/unzip.spec.js +++ b/tests/spec/shell/unzip.spec.js @@ -49,5 +49,35 @@ define(["Filer", "util"], function(Filer, util) { }); }); + it('should be able to zip and unzip a file', function(done) { + var fs = util.fs(); + var file = '/test-file.txt'; + var zipfile = file + '.zip'; + var shell = fs.Shell(); + var contents = "This is a test file used in some of the tests.\n"; + + fs.writeFile(file, contents, function(err) { + if(err) throw err; + + shell.zip(zipfile, file, function(err) { + if(err) throw err; + + shell.rm(file, function(err) { + if(err) throw err; + + shell.unzip(zipfile, function(err) { + if(err) throw err; + + fs.readFile(file, 'utf8', function(err, data) { + if(err) throw err; + expect(data).to.equal(contents); + done(); + }); + }); + }); + }); + }); + }); + }); });