Skip to content

Commit

Permalink
fix general purpose bit flag & add data descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ukyo committed Jun 15, 2016
1 parent f848afa commit 99255a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsziptools",
"main": "jsziptools.js",
"version": "2.4.6",
"version": "2.4.7",
"homepage": "https://github.com/ukyo/jsziptools",
"authors": [
"ukyo <[email protected]>"
Expand Down
6 changes: 3 additions & 3 deletions jsziptools.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsziptools",
"version": "2.4.6",
"version": "2.4.7",
"description": "It's a utility of zlib, gzip and zip format binary data.",
"main": "jsziptools.js",
"directories": {
Expand Down
29 changes: 25 additions & 4 deletions src/ZipArchiveWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ZipArchiveWriter.prototype.writeFile = function(path, buffer, level) {
var offset = this.offset,
localFileHeader = createLocalFileHeader(path, this.date, level),
compressedSize = 0,
dataDescriptor,
crc32,
self = this;
this.trigger('data', localFileHeader);
if (level) {
Expand All @@ -87,8 +89,11 @@ ZipArchiveWriter.prototype.writeFile = function(path, buffer, level) {
compressedSize = buffer.length;
this.trigger('data', buffer);
}
this.centralDirHeaders.push(createCentralDirHeader(path, this.date, level, offset, buffer.length, compressedSize, algorithms.crc32(buffer)));
this.offset += localFileHeader.length + compressedSize;
crc32 = algorithms.crc32(buffer);
dataDescriptor = createDataDescriptor(crc32, compressedSize, buffer.length)
this.trigger('data', dataDescriptor);
this.centralDirHeaders.push(createCentralDirHeader(path, this.date, level, offset, buffer.length, compressedSize, crc32));
this.offset += localFileHeader.length + compressedSize + dataDescriptor.length;
return this;
};

Expand Down Expand Up @@ -145,7 +150,7 @@ function createLocalFileHeader(fileName, date, isDeflated) {
offset = 0;
view.setUint32(offset, zip.LOCAL_FILE_SIGNATURE, true); offset += 4; // local file header signature
view.setUint16(offset, 20, true); offset += 2; // version needed to extract
view.setUint16(offset, 0x0008); offset += 2; // general purpose bit flag
view.setUint16(offset, 0x0808); offset += 2; // general purpose bit flag
view.setUint16(offset, isDeflated ? 8 : 0, true); offset += 2; // compression method
view.setUint16(offset, createDosFileTime(date), true); offset += 2; // last mod file time
view.setUint16(offset, createDosFileDate(date), true); offset += 2; // last mod file date
Expand All @@ -160,6 +165,22 @@ function createLocalFileHeader(fileName, date, isDeflated) {
return bytes;
}

/**
* data descriptor
* @param {number} crc32
* @param {number} compressedSize
* @param {number} uncompressedSize
* @return {Uint8Array}
*/
function createDataDescriptor(crc32, compressedSize, uncompressedSize) {
var view = new DataView(new ArrayBuffer(16));
view.setUint32(0, zip.DATA_DESCRIPTOR_SIGNATURE, true);
view.setUint32(4, crc32, true);
view.setUint32(8, compressedSize, true);
view.setUint32(12, uncompressedSize, true);
return new Uint8Array(view.buffer);
}

/**
* central directory
* @param {string} fileName
Expand All @@ -176,7 +197,7 @@ function createCentralDirHeader(fileName, date, isDeflated, fileOffset, uncompre
view.setUint32(offset, zip.CENTRAL_DIR_SIGNATURE, true); offset += 4; // central file header signature
view.setUint16(offset, 20, true); offset += 2; // version made by (2.0)
view.setUint16(offset, 20, true); offset += 2; // version needed to extract
view.setUint16(offset, 0x0008); offset += 2; // general purpose bit flag (use utf8, data discriptor)
view.setUint16(offset, 0x0808); offset += 2; // general purpose bit flag (use utf8, data discriptor)
view.setUint16(offset, isDeflated ? 8 : 0, true); offset += 2; // compression method
view.setUint16(offset, createDosFileTime(date), true); offset += 2; // last mod file time
view.setUint16(offset, createDosFileDate(date), true); offset += 2; // last mod file date
Expand Down
1 change: 1 addition & 0 deletions src/jsziptools.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 99255a6

Please sign in to comment.