Adding sh.zip() but still failing a test

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-25 17:11:31 -04:00
parent 3411ba2dd3
commit fe4b1e5c0e
3 changed files with 84 additions and 1 deletions

View File

@ -136,7 +136,7 @@ define(function(require) {
} }
function getData(callback) { 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) { function writeUint8Array(array, callback, onerror) {

View File

@ -514,6 +514,59 @@ define(function(require) {
}, callback); }, 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; return Shell;
}); });

View File

@ -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();
});
});
});
});
});
});
}); });
}); });