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;
|
FileReader.prototype.checkCrc32 = false;
|
||||||
|
|
||||||
// Filer fs-based File Writer
|
// Filer fs-based File Writer
|
||||||
function FileWriter(path, fs, size) {
|
function FileWriter(path, fs) {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
function init(callback) {
|
function init(callback, onerror) {
|
||||||
// TODO: Overwrite the file. Should we also have a way to fail if exists?
|
// XXX: delete path if it exists?
|
||||||
fs.unlink(path, function() {
|
fs.Shell().rm(path, function(err) {
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getData(callback) {
|
function getData(callback, onerror) {
|
||||||
callback(); // XXX I don't think I need getData to return anything... that.data);
|
fs.readFile(path, function(err, data) {
|
||||||
|
if(err) return onerror(err);
|
||||||
|
callback(data);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeUint8Array(array, callback, onerror) {
|
function writeUint8Array(array, callback, onerror) {
|
||||||
fs.appendFile(path, array, function(err) {
|
fs.appendFile(path, array, function(err) {
|
||||||
if(err) return onerror(err);
|
if(err) return onerror(err);
|
||||||
that.data = array;
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
that.size = size;
|
|
||||||
that.init = init;
|
that.init = init;
|
||||||
that.writeUint8Array = writeUint8Array;
|
that.writeUint8Array = writeUint8Array;
|
||||||
that.getData = getData;
|
that.getData = getData;
|
||||||
|
|
|
@ -502,7 +502,8 @@ define(function(require) {
|
||||||
if(entry.directory) {
|
if(entry.directory) {
|
||||||
this.mkdirp(path, callback);
|
this.mkdirp(path, callback);
|
||||||
} else {
|
} 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);
|
zipfile = Path.resolve(this.cwd, zipfile);
|
||||||
|
|
||||||
var Zip = require('zip.js/zip');
|
var Zip = require('zip.js/zip');
|
||||||
|
function zipPath(path, callback) {
|
||||||
Zip.createWriter(new Zip.FileWriter(zipfile, fs), function(writer) {
|
Zip.createWriter(new Zip.FileWriter(zipfile, fs), function(writer) {
|
||||||
|
|
||||||
function add(path, callback) {
|
|
||||||
var realpath = Path.resolve(this.cwd, path);
|
var realpath = Path.resolve(this.cwd, path);
|
||||||
fs.stat(realpath, function(err, stats) {
|
fs.stat(realpath, function(err, stats) {
|
||||||
if(err) {
|
if(err) {
|
||||||
|
@ -554,17 +554,12 @@ define(function(require) {
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.add(path, new Zip.FileReader(realpath, fs),
|
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) {
|
async.eachSeries(paths, zipPath, callback);
|
||||||
if(error) return callback(error);
|
|
||||||
writer.close(function() {
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return Shell;
|
return Shell;
|
||||||
|
|
|
@ -51,24 +51,28 @@ define(["Filer", "util"], function(Filer, util) {
|
||||||
|
|
||||||
it('should be able to zip and unzip a file', function(done) {
|
it('should be able to zip and unzip a file', function(done) {
|
||||||
var fs = util.fs();
|
var fs = util.fs();
|
||||||
var file = '/test-file.txt';
|
var file = 'test-file.txt';
|
||||||
var zipfile = file + '.zip';
|
var zipfile = file + '.zip';
|
||||||
var shell = fs.Shell();
|
var shell = fs.Shell();
|
||||||
|
var Path = Filer.Path;
|
||||||
var contents = "This is a test file used in some of the tests.\n";
|
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;
|
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;
|
if(err) throw err;
|
||||||
|
|
||||||
shell.rm(file, function(err) {
|
shell.zip(zipfilepath, filepath, function(err) {
|
||||||
if(err) throw err;
|
if(err) throw err;
|
||||||
|
|
||||||
shell.unzip(zipfile, function(err) {
|
shell.unzip(zipfilepath, function(err) {
|
||||||
if(err) throw 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;
|
if(err) throw err;
|
||||||
expect(data).to.equal(contents);
|
expect(data).to.equal(contents);
|
||||||
done();
|
done();
|
||||||
|
|
Loading…
Reference in New Issue