diff --git a/adm-zip.js b/adm-zip.js index 0602374..9312fce 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -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 diff --git a/zipFile.js b/zipFile.js index 3a62b93..0e9c771 100644 --- a/zipFile.js +++ b/zipFile.js @@ -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 @@ -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 @@ -90,7 +110,7 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) { if (mainHeader.commentLength) { _comment = inBuffer.slice(commentEnd + Utils.Constants.ENDHDR); } - readEntries(); + // readEntries(); } return { @@ -99,6 +119,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) { * @return Array */ get entries() { + if (!loadedEntries) { + readEntries(); + } return entryList; }, @@ -114,6 +137,23 @@ 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 * @@ -121,6 +161,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) { * @return ZipEntry */ getEntry: function (/*String*/entryName) { + if (!loadedEntries) { + readEntries(); + } return entryTable[entryName] || null; }, @@ -130,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; @@ -142,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; @@ -163,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, @@ -184,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(); @@ -258,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();