Skip to content

Commit

Permalink
Add partial support for zip64 (larger number of entries)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Jan 21, 2020
1 parent ff17ae8 commit d7a748c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
40 changes: 29 additions & 11 deletions headers/mainHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@ module.exports = function () {

loadFromBinary : function(/*Buffer*/data) {
// data should be 22 bytes and start with "PK 05 06"
if (data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG)
// or be 56+ bytes and start with "PK 06 06" for Zip64
if ((data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG) &&
(data.length < Constants.ZIP64HDR || data.readUInt32LE(0) !== Constants.ZIP64SIG)) {

throw Utils.Errors.INVALID_END;
}

if (data.readUInt32LE(0) === Constants.ENDSIG) {
// number of entries on this volume
_volumeEntries = data.readUInt16LE(Constants.ENDSUB);
// total number of entries
_totalEntries = data.readUInt16LE(Constants.ENDTOT);
// central directory size in bytes
_size = data.readUInt32LE(Constants.ENDSIZ);
// offset of first CEN header
_offset = data.readUInt32LE(Constants.ENDOFF);
// zip file comment length
_commentLength = data.readUInt16LE(Constants.ENDCOM);
} else {
// number of entries on this volume
_volumeEntries = Utils.readBigUInt64LE(data, Constants.ZIP64SUB);
// total number of entries
_totalEntries = Utils.readBigUInt64LE(data, Constants.ZIP64TOT);
// central directory size in bytes
_size = Utils.readBigUInt64LE(data, Constants.ZIP64SIZ);
// offset of first CEN header
_offset = Utils.readBigUInt64LE(data, Constants.ZIP64OFF);

_commentLength = 0;
}

// number of entries on this volume
_volumeEntries = data.readUInt16LE(Constants.ENDSUB);
// total number of entries
_totalEntries = data.readUInt16LE(Constants.ENDTOT);
// central directory size in bytes
_size = data.readUInt32LE(Constants.ENDSIZ);
// offset of first CEN header
_offset = data.readUInt32LE(Constants.ENDOFF);
// zip file comment length
_commentLength = data.readUInt16LE(Constants.ENDCOM);
},

toBinary : function() {
Expand Down
20 changes: 20 additions & 0 deletions util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ module.exports = {
ENDOFF : 16, // offset of first CEN header
ENDCOM : 20, // zip file comment length

END64HDR : 20, // zip64 END header size
END64SIG : 0x07064b50, // zip64 Locator signature, "PK\006\007"
END64START : 4, // number of the disk with the start of the zip64
END64OFF : 8, // relative offset of the zip64 end of central directory
END64NUMDISKS : 16, // total number of disks

ZIP64SIG : 0x06064b50, // zip64 signature, "PK\006\006"
ZIP64HDR : 56, // zip64 record minimum size
ZIP64LEAD : 12, // leading bytes at the start of the record, not counted by the value stored in ZIP64SIZE
ZIP64SIZE : 4, // zip64 size of the central directory record
ZIP64VEM : 12, // zip64 version made by
ZIP64VER : 14, // zip64 version needed to extract
ZIP64DSK : 16, // zip64 number of this disk
ZIP64DSKDIR : 20, // number of the disk with the start of the record directory
ZIP64SUB : 24, // number of entries on this disk
ZIP64TOT : 32, // total number of entries
ZIP64SIZB : 40, // zip64 central directory size in bytes
ZIP64OFF : 48, // offset of start of central directory with respect to the starting disk number
ZIP64EXTRA : 56, // extensible data sector

/* Compression methods */
STORED : 0, // no compression
SHRUNK : 1, // shrunk
Expand Down
9 changes: 9 additions & 0 deletions util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ module.exports = (function() {
return files;
}

function readBigUInt64LE(/*Buffer*/buffer, /*int*/index) {
var slice = Buffer.from(buffer.slice(index, index + 8));
slice.swap64();

return parseInt(`0x${ slice.toString('hex') }`);
}

return {
makeDir : function(/*String*/path) {
mkdirSync(path);
Expand Down Expand Up @@ -203,6 +210,8 @@ module.exports = (function() {
}
},

readBigUInt64LE,

Constants : Constants,
Errors : Errors
}
Expand Down
29 changes: 25 additions & 4 deletions zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,43 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {

function readMainHeader() {
var i = inBuffer.length - Utils.Constants.ENDHDR, // END header size
n = Math.max(0, i - 0xFFFF), // 0xFFFF is the max zip file comment length
endOffset = -1; // Start offset of the END header
max = Math.max(0, i - 0xFFFF), // 0xFFFF is the max zip file comment length
n = max,
endStart = inBuffer.length,
endOffset = -1, // Start offset of the END header
commentEnd = 0;

for (i; i >= n; i--) {
if (inBuffer[i] !== 0x50) continue; // quick check that the byte is 'P'
if (inBuffer.readUInt32LE(i) === Utils.Constants.ENDSIG) { // "PK\005\006"
endOffset = i;
commentEnd = i;
endStart = i + Utils.Constants.ENDHDR;
// We already found a regular signature, let's look just a bit further to check if there's any zip64 signature
n = i - Utils.Constants.END64HDR;
continue;
}

if (inBuffer.readUInt32LE(i) === Utils.Constants.END64SIG) {
// Found a zip64 signature, let's continue reading the whole zip64 record
n = max;
continue;
}

if (inBuffer.readUInt32LE(i) == Utils.Constants.ZIP64SIG) {
// Found the zip64 record, let's determine it's size
endOffset = i;
endStart = i + Utils.readBigUInt64LE(inBuffer, i + Utils.Constants.ZIP64SIZE) + Utils.Constants.ZIP64LEAD;
break;
}
}

if (!~endOffset)
throw Utils.Errors.INVALID_FORMAT;

mainHeader.loadFromBinary(inBuffer.slice(endOffset, endOffset + Utils.Constants.ENDHDR));
mainHeader.loadFromBinary(inBuffer.slice(endOffset, endStart));
if (mainHeader.commentLength) {
_comment = inBuffer.slice(endOffset + Utils.Constants.ENDHDR);
_comment = inBuffer.slice(commentEnd + Utils.Constants.ENDHDR);
}
readEntries();
}
Expand Down

0 comments on commit d7a748c

Please sign in to comment.