Skip to content

Commit f9d1a3a

Browse files
committed
added some functions to handle entries
1 parent 161a61f commit f9d1a3a

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

adm-zip.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ module.exports = function (/**String*/ input, /** object */ options) {
123123
return (item && item.getData(pass)) || null;
124124
},
125125

126+
/**
127+
* Returns how many child elements has on entry (directories) on files it is always 0
128+
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
129+
* @returns {integer}
130+
*/
131+
childCount: function (entry) {
132+
const item = getEntry(entry);
133+
if (item) {
134+
return _zip.getChildCount(item);
135+
}
136+
},
137+
126138
/**
127139
* Asynchronous readFile
128140
* @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) {
188200
/**
189201
* Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
190202
*
191-
* @param {ZipEntry} entry
203+
* @param {ZipEntry|string} entry
204+
* @returns {void}
192205
*/
193206
deleteFile: function (entry) {
194207
// @TODO: test deleteFile
195208
var item = getEntry(entry);
209+
if (item) {
210+
_zip.deleteFile(item.entryName);
211+
}
212+
},
213+
214+
/**
215+
* Remove the entry from the file or directory without affecting any nested entries
216+
*
217+
* @param {ZipEntry|string} entry
218+
* @returns {void}
219+
*/
220+
deleteEntry: function (entry) {
221+
// @TODO: test deleteEntry
222+
var item = getEntry(entry);
196223
if (item) {
197224
_zip.deleteEntry(item.entryName);
198225
}

zipFile.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
190190
},
191191

192192
/**
193-
* Removes the entry with the given name from the entry list.
193+
* Removes the file with the given name from the entry list.
194194
*
195195
* If the entry is a directory, then all nested files and directories will be removed
196196
* @param entryName
197+
* @returns {void}
197198
*/
198-
deleteEntry: function (/*String*/ entryName) {
199+
deleteFile: function (/*String*/ entryName) {
199200
if (!loadedEntries) {
200201
readEntries();
201202
}
@@ -204,7 +205,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
204205
var _self = this;
205206
this.getEntryChildren(entry).forEach(function (child) {
206207
if (child.entryName !== entryName) {
207-
_self.deleteEntry(child.entryName);
208+
_self.deleteFile(child.entryName);
208209
}
209210
});
210211
}
@@ -213,6 +214,22 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
213214
mainHeader.totalEntries = entryList.length;
214215
},
215216

217+
/**
218+
* Removes the entry with the given name from the entry list.
219+
*
220+
* @param {string} entryName
221+
* @returns {void}
222+
*/
223+
deleteEntry: function (/*String*/ entryName) {
224+
if (!loadedEntries) {
225+
readEntries();
226+
}
227+
const entry = entryTable[entryName];
228+
entryList.splice(entryList.indexOf(entry), 1);
229+
delete entryTable[entryName];
230+
mainHeader.totalEntries = entryList.length;
231+
},
232+
216233
/**
217234
* Iterates and returns all nested files and directories of the given entry
218235
*
@@ -238,6 +255,20 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
238255
return [];
239256
},
240257

258+
/**
259+
* How many child elements entry has
260+
*
261+
* @param {ZipEntry} entry
262+
* @return {integer}
263+
*/
264+
getChildCount: function (entry) {
265+
if (entry && entry.isDirectory) {
266+
const list = this.getEntryChildren(entry);
267+
return list.includes(entry) ? list.length - 1 : list.length;
268+
}
269+
return 0;
270+
},
271+
241272
/**
242273
* Returns the zip file
243274
*

0 commit comments

Comments
 (0)