Skip to content

Commit

Permalink
move ZipObject and JSZip attributes
Browse files Browse the repository at this point in the history
Now `date`, `dir` and `comment` are attributes of `ZipObject`.
See #137 for more details.
  • Loading branch information
dduponchel committed Jun 1, 2014
1 parent 3931db4 commit 1689016
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
3 changes: 1 addition & 2 deletions documentation/api_jszip.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ modify them. You can also import an existing zip file or generate one.
attribute name | type | description
---------------------|-------------|-------------
`files` | object | the [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html)s inside the zip with the name as key. See [file(name)]({{site.baseurl}}/documentation/api_jszip/file_name.html).
`options` | object | the options of the zip. The available options are :
`options.comment` | string | the comment of the zip file.
`comment` | string | the comment of the zip file.
11 changes: 7 additions & 4 deletions documentation/api_zipobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ will be automatically decompressed/converted first.
attribute name | type | description
---------------------|-------------|-------------
`name` | string | the absolute path of the file
`dir` | boolean | true if this is a directory
`date` | date | the last modification date
`comment` | string | the comment for this file
`options` | object | the options of the file. The available options are :
`options.base64` | boolean | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.binary` | boolean | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.dir` | boolean | true if this is a directory
`options.date` | date | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.base64` | boolean | **Deprecated**, see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.binary` | boolean | **Deprecated**, see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.dir` | boolean | **Deprecated**, use `dir`. True if this is a directory
`options.date` | date | **Deprecated**, use `date`. See [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)
`options.compression`| compression | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html)


Expand Down
4 changes: 1 addition & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function JSZip(data, options) {
// }
this.files = {};

this.options = {
comment : null
};
this.comment = null;

// Where we are in the hierarchy
this.root = "";
Expand Down
2 changes: 1 addition & 1 deletion lib/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(data, options) {
});
}
if (zipEntries.zipComment.length) {
this.options.comment = zipEntries.zipComment;
this.comment = zipEntries.zipComment;
}

return this;
Expand Down
34 changes: 19 additions & 15 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ var dataToString = function(asUTF8) {
*/
var ZipObject = function(name, data, options) {
this.name = name;
this.dir = options.dir;
this.date = options.date;
this.comment = options.comment;

this._data = data;
this.options = options;
};
Expand Down Expand Up @@ -287,7 +291,7 @@ var generateCompressedObjectFrom = function(file, compression) {
result.uncompressedSize = file._data.uncompressedSize;
result.crc32 = file._data.crc32;

if (result.uncompressedSize === 0 || file.options.dir) {
if (result.uncompressedSize === 0 || file.dir) {
compression = compressions['STORE'];
result.compressedContent = "";
result.crc32 = 0;
Expand All @@ -304,7 +308,7 @@ var generateCompressedObjectFrom = function(file, compression) {
else {
// have uncompressed data
content = getBinaryData(file);
if (!content || content.length === 0 || file.options.dir) {
if (!content || content.length === 0 || file.dir) {
compression = compressions['STORE'];
content = "";
}
Expand All @@ -330,7 +334,7 @@ var generateCompressedObjectFrom = function(file, compression) {
var generateZipParts = function(name, file, compressedObject, offset) {
var data = compressedObject.compressedContent,
utfEncodedFileName = this.utf8encode(file.name),
comment = file.options.comment || "",
comment = file.comment || "",
utfEncodedComment = this.utf8encode(comment),
useUTF8ForFileName = utfEncodedFileName !== file.name,
useUTF8ForComment = utfEncodedComment !== comment,
Expand All @@ -346,17 +350,17 @@ var generateZipParts = function(name, file, compressedObject, offset) {
// @see http://www.delorie.com/djgpp/doc/rbinter/it/65/16.html
// @see http://www.delorie.com/djgpp/doc/rbinter/it/66/16.html

dosTime = o.date.getHours();
dosTime = file.date.getHours();
dosTime = dosTime << 6;
dosTime = dosTime | o.date.getMinutes();
dosTime = dosTime | file.date.getMinutes();
dosTime = dosTime << 5;
dosTime = dosTime | o.date.getSeconds() / 2;
dosTime = dosTime | file.date.getSeconds() / 2;

dosDate = o.date.getFullYear() - 1980;
dosDate = file.date.getFullYear() - 1980;
dosDate = dosDate << 4;
dosDate = dosDate | (o.date.getMonth() + 1);
dosDate = dosDate | (file.date.getMonth() + 1);
dosDate = dosDate << 5;
dosDate = dosDate | o.date.getDate();
dosDate = dosDate | file.date.getDate();

if (useUTF8ForFileName) {
// set the unicode path extra field. unzip needs at least one extra
Expand Down Expand Up @@ -443,7 +447,7 @@ var generateZipParts = function(name, file, compressedObject, offset) {
// internal file attributes TODO
"\x00\x00" +
// external file attributes
(file.options.dir === true ? "\x10\x00\x00\x00" : "\x00\x00\x00\x00") +
(file.dir === true ? "\x10\x00\x00\x00" : "\x00\x00\x00\x00") +
// relative offset of local header
decToHex(offset, 4) +
// file name
Expand Down Expand Up @@ -569,12 +573,12 @@ var out = {
if (utils.isRegExp(name)) {
var regexp = name;
return this.filter(function(relativePath, file) {
return !file.options.dir && regexp.test(relativePath);
return !file.dir && regexp.test(relativePath);
});
}
else { // text
return this.filter(function(relativePath, file) {
return !file.options.dir && relativePath === name;
return !file.dir && relativePath === name;
})[0] || null;
}
}
Expand All @@ -597,7 +601,7 @@ var out = {

if (utils.isRegExp(arg)) {
return this.filter(function(relativePath, file) {
return file.options.dir && arg.test(relativePath);
return file.dir && arg.test(relativePath);
});
}

Expand Down Expand Up @@ -627,7 +631,7 @@ var out = {
file = this.files[name];
}

if (file && !file.options.dir) {
if (file && !file.dir) {
// file
delete this.files[name];
} else {
Expand Down Expand Up @@ -665,7 +669,7 @@ var out = {
localDirLength = 0,
centralDirLength = 0,
writer, i,
utfEncodedComment = this.utf8encode(options.comment || this.options.comment || "");
utfEncodedComment = this.utf8encode(options.comment || this.comment || "");

// first, generate all the zip parts.
for (var name in this.files) {
Expand Down
39 changes: 33 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ testZipFile("Finding a file : modifying the result doesn't alter the zip", "ref/
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
zip.file("Hello.txt").name = "Hello2.txt";
zip.file("Hello.txt").options.dir = true;
zip.file("Hello.txt").dir = true;
// these changes won't be used
var content = zip.generate();

Expand Down Expand Up @@ -308,6 +308,31 @@ test("Finding folders with relative path", function () {
equal(root.folder(/^subsub1/).length, 1, "relative folder path is used");
equal(root.folder(/root/).length, 0, "parent folder is not matched");
});

function zipObjectsAssertions(zipObject) {
var date = new Date("July 17, 2009 14:36:57");

equal(zipObject.name, "Hello.txt", "ZipObject#name is here");

equal(zipObject.comment, "my comment", "ZipObject#comment is here");

// the zip date has a 2s resolution
var delta = Math.abs(zipObject.date.getTime() - date.getTime());
ok(delta < 2000/* ms */, date, "ZipObject#date is here");
var deltaOptions = Math.abs(zipObject.options.date.getTime() - date.getTime());
ok(deltaOptions < 2000/* ms */, date, "ZipObject#options.date is here (deprecated API)");
}
test("ZipObject attributes", function () {
var date = new Date("July 17, 2009 14:36:57");
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n", {comment:"my comment", date:date});
zipObjectsAssertions(zip.file("Hello.txt"));
zipObjectsAssertions(zip.files["Hello.txt"]);
var reloaded = new JSZip(zip.generate({base64:false}));
zipObjectsAssertions(reloaded.file("Hello.txt"));
zipObjectsAssertions(reloaded.files["Hello.txt"]);
});

// }}} module Essential

QUnit.module("More advanced"); // {{{
Expand Down Expand Up @@ -818,7 +843,7 @@ testZipFile("Filtering a zip : the filter function can't alter the data", "ref/t
zip.filter(function (relativeFilename, file) {
file.name = "bye.txt";
file.data = "good bye";
file.options.dir = true;
file.dir = true;
});
var content = zip.generate();

Expand Down Expand Up @@ -1042,9 +1067,9 @@ testZipFile("zip with DEFLATE", "ref/deflate.zip", function(file) {
// zip -0 -X -z -c archive_comment.zip Hello.txt
testZipFile("read zip with comment", "ref/archive_comment.zip", function(file) {
var zip = new JSZip(file);
equal(zip.options.comment, "file comment", "the archive comment was correctly read.");
equal(zip.comment, "file comment", "the archive comment was correctly read.");
equal(zip.file("Hello.txt").asText(), "Hello World\n", "the zip was correctly read.");
equal(zip.file("Hello.txt").options.comment, "entry comment", "the entry comment was correctly read.");
equal(zip.file("Hello.txt").comment, "entry comment", "the entry comment was correctly read.");
});
testZipFile("generate zip with comment", "ref/archive_comment.zip", function(file) {
var zip = new JSZip();
Expand Down Expand Up @@ -1138,9 +1163,11 @@ testZipFile("Zip text file from windows with \\ in central dir", "ref/slashes_an
test("A folder stays a folder", function () {
var zip = new JSZip();
zip.folder("folder/");
ok(zip.files['folder/'].options.dir, "the folder is marked as a folder");
ok(zip.files['folder/'].dir, "the folder is marked as a folder");
ok(zip.files['folder/'].options.dir, "the folder is marked as a folder, deprecated API");
var reloaded = new JSZip(zip.generate({base64:false}));
ok(reloaded.files['folder/'].options.dir, "the folder is marked as a folder");
ok(reloaded.files['folder/'].dir, "the folder is marked as a folder");
ok(reloaded.files['folder/'].options.dir, "the folder is marked as a folder, deprecated API");
});

// }}} Load file
Expand Down

0 comments on commit 1689016

Please sign in to comment.