Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't generate subfolders. #136

Merged
merged 1 commit into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 11 additions & 33 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,7 @@ var prepareFileAttrs = function(o) {
*/
var fileAdd = function(name, data, o) {
// be sure sub folders exist
var parent = parentFolder(name),
dataType = utils.getTypeOf(data);
if (parent) {
folderAdd.call(this, parent);
}
var dataType = utils.getTypeOf(data);

o = prepareFileAttrs(o);

Expand Down Expand Up @@ -255,21 +251,6 @@ var fileAdd = function(name, data, o) {
return object;
};


/**
* Find the parent folder of the path.
* @private
* @param {string} path the path to use
* @return {string} the parent folder, or ""
*/
var parentFolder = function(path) {
if (path.slice(-1) == '/') {
path = path.substring(0, path.length - 1);
}
var lastSlash = path.lastIndexOf('/');
return (lastSlash > 0) ? path.substring(0, lastSlash) : "";
};

/**
* Add a (sub) folder in the current folder.
* @private
Expand Down Expand Up @@ -622,19 +603,16 @@ var out = {
file = this.files[name];
}

if (file) {
if (!file.options.dir) {
// file
delete this.files[name];
}
else {
// folder
var kids = this.filter(function(relativePath, file) {
return file.name.slice(0, name.length) === name;
});
for (var i = 0; i < kids.length; i++) {
delete this.files[kids[i].name];
}
if (file && !file.options.dir) {
// file
delete this.files[name];
} else {
// maybe a folder, delete recursively
var kids = this.filter(function(relativePath, file) {
return file.name.slice(0, name.length) === name;
});
for (var i = 0; i < kids.length; i++) {
delete this.files[kids[i].name];
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ test("Finding folders", function () {
zip.folder("root/").folder("sub1/");
zip.folder("root/sub2/subsub1");

equal(zip.folder(/sub2\/$/).length, 1, "unique result");
equal(zip.folder(/sub2\/$/).length, 0, "unique result");
equal(zip.folder(/sub1/).length, 2, "multiple results");
equal(zip.folder(/root/).length, 4, "match on whole path");
equal(zip.folder(/root/).length, 3, "match on whole path");
});

test("Finding folders with relative path", function () {
Expand Down Expand Up @@ -1152,25 +1152,25 @@ if (QUnit.urlParams.complexfiles) {
// a showcase in http://msdn.microsoft.com/en-us/windows/hardware/gg463429
testZipFile("Outlook2007_Calendar.xps", "ref/complex_files/Outlook2007_Calendar.xps", function(file) {
var zip = new JSZip(file);
// the zip file contains 15 entries, but we get 23 when creating all the sub-folders.
equal(zip.filter(function(){return true;}).length, 23, "the zip contains the good number of elements.");
// the zip file contains 15 entries.
equal(zip.filter(function(){return true;}).length, 15, "the zip contains the good number of elements.");
ok(zip.file("[Content_Types].xml").asText().indexOf("application/vnd.ms-package.xps-fixeddocument+xml") !== -1, "the zip was correctly read.");
});

// an example file in http://cheeso.members.winisp.net/srcview.aspx?dir=js-unzip
// the data come from http://www.antarctica.ac.uk/met/READER/upper_air/
testZipFile("AntarcticaTemps.xlsx", "ref/complex_files/AntarcticaTemps.xlsx", function(file) {
var zip = new JSZip(file);
// the zip file contains 16 entries, but we get 27 when creating all the sub-folders.
equal(zip.filter(function(){return true;}).length, 27, "the zip contains the good number of elements.");
// the zip file contains 17 entries.
equal(zip.filter(function(){return true;}).length, 17, "the zip contains the good number of elements.");
ok(zip.file("[Content_Types].xml").asText().indexOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") !== -1, "the zip was correctly read.");
});

// same as above, but in the Open Document format
testZipFile("AntarcticaTemps.ods", "ref/complex_files/AntarcticaTemps.ods", function(file) {
var zip = new JSZip(file);
// the zip file contains 19 entries, but we get 27 when creating all the sub-folders.
equal(zip.filter(function(){return true;}).length, 27, "the zip contains the good number of elements.");
// the zip file contains 20 entries.
equal(zip.filter(function(){return true;}).length, 20, "the zip contains the good number of elements.");
ok(zip.file("META-INF/manifest.xml").asText().indexOf("application/vnd.oasis.opendocument.spreadsheet") !== -1, "the zip was correctly read.");
});
}
Expand Down