Work thus far, still not right
This commit is contained in:
parent
1cbede711a
commit
c9cb4b4bdb
|
@ -125,29 +125,30 @@ define(function(require) {
|
|||
FileReader.prototype.checkCrc32 = false;
|
||||
|
||||
// Filer fs-based File Writer
|
||||
function FileWriter(path, fs, size) {
|
||||
function FileWriter(path, fs) {
|
||||
var that = this;
|
||||
|
||||
function init(callback) {
|
||||
// TODO: Overwrite the file. Should we also have a way to fail if exists?
|
||||
fs.unlink(path, function() {
|
||||
function init(callback, onerror) {
|
||||
// XXX: delete path if it exists?
|
||||
fs.Shell().rm(path, function(err) {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function getData(callback) {
|
||||
callback(); // XXX I don't think I need getData to return anything... that.data);
|
||||
function getData(callback, onerror) {
|
||||
fs.readFile(path, function(err, data) {
|
||||
if(err) return onerror(err);
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
|
||||
function writeUint8Array(array, callback, onerror) {
|
||||
fs.appendFile(path, array, function(err) {
|
||||
if(err) return onerror(err);
|
||||
that.data = array;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
that.size = size;
|
||||
that.init = init;
|
||||
that.writeUint8Array = writeUint8Array;
|
||||
that.getData = getData;
|
||||
|
|
|
@ -502,7 +502,8 @@ define(function(require) {
|
|||
if(entry.directory) {
|
||||
this.mkdirp(path, callback);
|
||||
} else {
|
||||
entry.getData(new Zip.FileWriter(path, fs, entry.uncompressedSize), function() { callback(); });
|
||||
console.log('inflate', path);
|
||||
entry.getData(new Zip.FileWriter(path, fs), function() { callback(); });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -537,9 +538,8 @@ define(function(require) {
|
|||
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) {
|
||||
function zipPath(path, callback) {
|
||||
Zip.createWriter(new Zip.FileWriter(zipfile, fs), function(writer) {
|
||||
var realpath = Path.resolve(this.cwd, path);
|
||||
fs.stat(realpath, function(err, stats) {
|
||||
if(err) {
|
||||
|
@ -554,17 +554,12 @@ define(function(require) {
|
|||
}
|
||||
|
||||
writer.add(path, new Zip.FileReader(realpath, fs),
|
||||
function onend() { callback(); }, null, options);
|
||||
function() { writer.close(function() { callback(); }); }, null, options);
|
||||
});
|
||||
}
|
||||
}, function(err) { callback(err); });
|
||||
}
|
||||
|
||||
async.eachSeries(paths, add, function(error) {
|
||||
if(error) return callback(error);
|
||||
writer.close(function() {
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}, callback);
|
||||
async.eachSeries(paths, zipPath, callback);
|
||||
};
|
||||
|
||||
return Shell;
|
||||
|
|
|
@ -51,24 +51,28 @@ 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 file = 'test-file.txt';
|
||||
var zipfile = file + '.zip';
|
||||
var shell = fs.Shell();
|
||||
var Path = Filer.Path;
|
||||
var contents = "This is a test file used in some of the tests.\n";
|
||||
|
||||
fs.writeFile(file, contents, function(err) {
|
||||
shell.tempDir(function(err, tmp) {
|
||||
if(err) throw err;
|
||||
|
||||
shell.zip(zipfile, file, function(err) {
|
||||
var filepath = Path.join(tmp, file);
|
||||
var zipfilepath = Path.join('/', zipfile);
|
||||
|
||||
fs.writeFile(filepath, contents, function(err) {
|
||||
if(err) throw err;
|
||||
|
||||
shell.rm(file, function(err) {
|
||||
shell.zip(zipfilepath, filepath, function(err) {
|
||||
if(err) throw err;
|
||||
|
||||
shell.unzip(zipfile, function(err) {
|
||||
shell.unzip(zipfilepath, function(err) {
|
||||
if(err) throw err;
|
||||
|
||||
fs.readFile(file, 'utf8', function(err, data) {
|
||||
fs.readFile(Path.join('/', file), 'utf8', function(err, data) {
|
||||
if(err) throw err;
|
||||
expect(data).to.equal(contents);
|
||||
done();
|
||||
|
|
Loading…
Reference in New Issue