Skip to content

Commit

Permalink
added some functions to handle entries
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Jun 25, 2024
1 parent 161a61f commit f9d1a3a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
29 changes: 28 additions & 1 deletion adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ module.exports = function (/**String*/ input, /** object */ options) {
return (item && item.getData(pass)) || null;
},

/**
* Returns how many child elements has on entry (directories) on files it is always 0
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
* @returns {integer}
*/
childCount: function (entry) {
const item = getEntry(entry);
if (item) {
return _zip.getChildCount(item);
}
},

/**
* Asynchronous readFile
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
Expand Down Expand Up @@ -188,11 +200,26 @@ module.exports = function (/**String*/ input, /** object */ options) {
/**
* Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
*
* @param {ZipEntry} entry
* @param {ZipEntry|string} entry
* @returns {void}
*/
deleteFile: function (entry) {
// @TODO: test deleteFile
var item = getEntry(entry);
if (item) {
_zip.deleteFile(item.entryName);
}
},

/**
* Remove the entry from the file or directory without affecting any nested entries
*
* @param {ZipEntry|string} entry
* @returns {void}
*/
deleteEntry: function (entry) {
// @TODO: test deleteEntry
var item = getEntry(entry);
if (item) {
_zip.deleteEntry(item.entryName);
}
Expand Down
37 changes: 34 additions & 3 deletions zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
},

/**
* Removes the entry with the given name from the entry list.
* Removes the file with the given name from the entry list.
*
* If the entry is a directory, then all nested files and directories will be removed
* @param entryName
* @returns {void}
*/
deleteEntry: function (/*String*/ entryName) {
deleteFile: function (/*String*/ entryName) {
if (!loadedEntries) {
readEntries();
}
Expand All @@ -204,7 +205,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
var _self = this;
this.getEntryChildren(entry).forEach(function (child) {
if (child.entryName !== entryName) {
_self.deleteEntry(child.entryName);
_self.deleteFile(child.entryName);
}
});
}
Expand All @@ -213,6 +214,22 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
mainHeader.totalEntries = entryList.length;
},

/**
* Removes the entry with the given name from the entry list.
*
* @param {string} entryName
* @returns {void}
*/
deleteEntry: function (/*String*/ entryName) {
if (!loadedEntries) {
readEntries();
}
const entry = entryTable[entryName];
entryList.splice(entryList.indexOf(entry), 1);
delete entryTable[entryName];
mainHeader.totalEntries = entryList.length;
},

/**
* Iterates and returns all nested files and directories of the given entry
*
Expand All @@ -238,6 +255,20 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
return [];
},

/**
* How many child elements entry has
*
* @param {ZipEntry} entry
* @return {integer}
*/
getChildCount: function (entry) {
if (entry && entry.isDirectory) {
const list = this.getEntryChildren(entry);
return list.includes(entry) ? list.length - 1 : list.length;
}
return 0;
},

/**
* Returns the zip file
*
Expand Down

0 comments on commit f9d1a3a

Please sign in to comment.