From fe4b1e5c0e88e4419e717d78ac5b8285fd15d9e9 Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Tue, 25 Mar 2014 17:11:31 -0400 Subject: [PATCH] Adding sh.zip() but still failing a test --- lib/zip.js/zip.js | 2 +- src/shell/shell.js | 53 ++++++++++++++++++++++++++++++++++ tests/spec/shell/unzip.spec.js | 30 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) 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(); + }); + }); + }); + }); + }); + }); + }); });