diff --git a/adm-zip.js b/adm-zip.js index a445dae..97fdcce 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -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 @@ -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); } diff --git a/zipFile.js b/zipFile.js index 8919632..d269e1c 100644 --- a/zipFile.js +++ b/zipFile.js @@ -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(); } @@ -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); } }); } @@ -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 * @@ -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 *