Skip to content

Commit

Permalink
Merge pull request #301 from RocketChat/zip64
Browse files Browse the repository at this point in the history
[WIP] Zip64 files and forEach method
  • Loading branch information
cthackers committed Feb 6, 2020
2 parents ff17ae8 + 0a4e8ca commit 8176b02
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 17 deletions.
8 changes: 8 additions & 0 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ module.exports = function (/*String*/input) {
return getEntry(name);
},

getEntryCount: function() {
return _zip.getEntryCount();
},

forEach: function(callback) {
return _zip.forEach(callback);
},

/**
* Extracts the given entry to the given targetPath
* If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted
Expand Down
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
91 changes: 85 additions & 6 deletions zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
filename = "",
fs = Utils.FileSystem.require(),
inBuffer = null,
mainHeader = new Headers.MainHeader();
mainHeader = new Headers.MainHeader(),
loadedEntries = false;

if (inputType === Utils.Constants.FILE) {
// is a filename
Expand All @@ -22,9 +23,28 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
readMainHeader();
} else {
// none. is a new file
loadedEntries = true;
}

function iterateEntries(callback) {
const totalEntries = mainHeader.diskEntries; // total number of entries
let index = mainHeader.offset; // offset of first CEN header

for (let i = 0; i < totalEntries; i++) {
let tmp = index;
const entry = new ZipEntry(inBuffer);

entry.header = inBuffer.slice(tmp, tmp += Utils.Constants.CENHDR);
entry.entryName = inBuffer.slice(tmp, tmp += entry.header.fileNameLength);

index += entry.header.entryHeaderSize;

callback(entry);
}
}

function readEntries() {
loadedEntries = true;
entryTable = {};
entryList = new Array(mainHeader.diskEntries); // total number of entries
var index = mainHeader.offset; // offset of first CEN header
Expand Down Expand Up @@ -52,24 +72,45 @@ 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();
// readEntries();
}

return {
Expand All @@ -78,6 +119,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
* @return Array
*/
get entries() {
if (!loadedEntries) {
readEntries();
}
return entryList;
},

Expand All @@ -93,13 +137,33 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
_comment = val;
},

getEntryCount: function() {
if (!loadedEntries) {
return mainHeader.diskEntries;
}

return entryList.length;
},

forEach: function(callback) {
if (!loadedEntries) {
iterateEntries(callback);
return;
}

entryList.forEach(callback);
},

/**
* Returns a reference to the entry with the given name or null if entry is inexistent
*
* @param entryName
* @return ZipEntry
*/
getEntry: function (/*String*/entryName) {
if (!loadedEntries) {
readEntries();
}
return entryTable[entryName] || null;
},

Expand All @@ -109,6 +173,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
* @param entry
*/
setEntry: function (/*ZipEntry*/entry) {
if (!loadedEntries) {
readEntries();
}
entryList.push(entry);
entryTable[entry.entryName] = entry;
mainHeader.totalEntries = entryList.length;
Expand All @@ -121,6 +188,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
* @param entryName
*/
deleteEntry: function (/*String*/entryName) {
if (!loadedEntries) {
readEntries();
}
var entry = entryTable[entryName];
if (entry && entry.isDirectory) {
var _self = this;
Expand All @@ -142,6 +212,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
* @return Array
*/
getEntryChildren: function (/*ZipEntry*/entry) {
if (!loadedEntries) {
readEntries();
}
if (entry.isDirectory) {
var list = [],
name = entry.entryName,
Expand All @@ -163,6 +236,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
* @return Buffer
*/
compressToBuffer: function () {
if (!loadedEntries) {
readEntries();
}
if (entryList.length > 1) {
entryList.sort(function (a, b) {
var nameA = a.entryName.toLowerCase();
Expand Down Expand Up @@ -237,6 +313,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
},

toAsyncBuffer: function (/*Function*/onSuccess, /*Function*/onFail, /*Function*/onItemStart, /*Function*/onItemEnd) {
if (!loadedEntries) {
readEntries();
}
if (entryList.length > 1) {
entryList.sort(function (a, b) {
var nameA = a.entryName.toLowerCase();
Expand Down

0 comments on commit 8176b02

Please sign in to comment.