Complex trees can be unzipped

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-26 14:28:50 -04:00
parent 16c64d1ca2
commit 35b2fe46c6
3 changed files with 69 additions and 8 deletions

View File

@ -501,23 +501,24 @@ define(function(require) {
if(err) return callback(err); if(err) return callback(err);
var unzip = new Zlib.Unzip(data); var unzip = new Zlib.Unzip(data);
// Separate filenames within the zip archive with what will go in fs
// Separate filenames within the zip archive with what will go in fs.
// Also mark any directories (i.e., paths with a trailing '/')
var filenames = unzip.getFilenames().map(function(filename) { var filenames = unzip.getFilenames().map(function(filename) {
return { return {
zipFilename: filename, zipFilename: filename,
fsFilename: Path.join(destination, filename) fsFilename: Path.join(destination, filename),
isDirectory: /\/$/.test(filename)
}; };
}); });
function decompress(path, callback) { function decompress(path, callback) {
var data = unzip.decompress(path.zipFilename); var data = unzip.decompress(path.zipFilename);
if(path.isDirectory) {
// Make sure the dir(s) we need exist before writing file sh.mkdirp(path.fsFilename, callback);
var dir = Path.dirname(path.fsFilename); } else {
sh.mkdirp(dir, function(err) {
console.log('inflate', path.zipFilename, path.fsFilename);
fs.writeFile(path.fsFilename, data, callback); fs.writeFile(path.fsFilename, data, callback);
}); }
} }
async.eachSeries(filenames, decompress, callback); async.eachSeries(filenames, decompress, callback);

View File

@ -90,5 +90,65 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
it('should be able to handle a deep tree structure in a zip', function(done) {
// test-dir.zip has the following structure:
//
// test-dir/
// ├── test-dir2
// │ └── test-file.txt
// ├── test-file.txt
// └── test-file2.txt
var fs = util.fs();
var shell = fs.Shell();
var url = "test-dir.zip";
var Path = Filer.Path;
var contents = "This is a test file used in some of the tests.\n";
function confirmFile(filename, callback) {
filename = Path.join('/', filename);
fs.readFile(filename, 'utf8', function(err, data) {
if(err) {
console.error('Expected ' + filename + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(data).to.equal(contents);
callback();
});
}
function confirmDir(dirname, callback) {
dirname = Path.join('/', dirname);
fs.stat(dirname, function(err, stats) {
if(err) {
console.error('Expected ' + dirname + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(stats.isDirectory()).to.be.true;
callback();
});
}
shell.wget(url, {filename: url}, function(err, path) {
if(err) throw err;
shell.unzip(path, function(err) {
if(err) throw err;
// Forgive the crazy indenting, trying to match tree structure ;)
confirmDir('test-dir', function() {
confirmDir('test-dir/test-dir2', function() {
confirmFile('test-dir/test-dir2/test-file.txt', function() {
confirmFile('test-dir/test-file.txt', function() {
confirmFile('test-dir/test-file2.txt', function() {
done();
});});});});});
});
});
});
}); });
}); });

BIN
tests/test-dir.zip Normal file

Binary file not shown.