Skip to content

Commit

Permalink
Iterate over entries without storing their metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Jan 21, 2020
1 parent d7a748c commit 0a4e8ca
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 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
62 changes: 60 additions & 2 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

This comment has been minimized.

Copy link
@puppeteer701vungle

puppeteer701vungle Feb 19, 2020

Most of the code is using var's only here we are using ECMA6. Is there a reason on why?


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

Expand All @@ -114,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 @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0a4e8ca

Please sign in to comment.