Skip to content

Commit db8ec3f

Browse files
committed
deleteFile is too eager
1 parent ef63cbb commit db8ec3f

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

adm-zip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ module.exports = function (/**String*/ input, /** object */ options) {
203203
* @param {ZipEntry|string} entry
204204
* @returns {void}
205205
*/
206-
deleteFile: function (entry) {
206+
deleteFile: function (entry, withsubfolders = true) {
207207
// @TODO: test deleteFile
208208
var item = getEntry(entry);
209209
if (item) {
210-
_zip.deleteFile(item.entryName);
210+
_zip.deleteFile(item.entryName, withsubfolders);
211211
}
212212
},
213213

test/methods/methods.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,87 @@ describe("adm-zip.js - methods handling local files", () => {
2828
// clean up folder content
2929
afterEach((done) => rimraf(destination, done));
3030

31+
describe(".deleteFile()", () => {
32+
const ultrazip = [
33+
"./attributes_test/asd/New Text Document.txt",
34+
"./attributes_test/blank file.txt",
35+
"./attributes_test/New folder/hidden.txt",
36+
"./attributes_test/New folder/hidden_readonly.txt",
37+
"./attributes_test/New folder/readonly.txt",
38+
"./utes_test/New folder/somefile.txt"
39+
].map(wrapList);
40+
41+
// Issue 523 - deletes additional files
42+
it("zip.deleteFile() - delete folder with subfolders", () => {
43+
const content = "test";
44+
const comment = "comment";
45+
const zip1 = new Zip({ noSort: true });
46+
zip1.addFile("test/");
47+
zip1.addFile("test/path1/");
48+
zip1.addFile("test/path1/file1.txt", content, comment);
49+
zip1.addFile("test/path1/folder1/");
50+
zip1.addFile("test/path1/folder1/file2.txt", content, comment);
51+
zip1.addFile("test/path2/");
52+
zip1.addFile("test/path2/file1.txt", content, comment);
53+
zip1.addFile("test/path2/folder1/");
54+
zip1.addFile("test/path2/folder1/file2.txt", content, comment);
55+
56+
zip1.deleteFile("test/path1/");
57+
58+
const zipEntries = zip1.getEntries().map((child) => child.entryName);
59+
60+
expect(zipEntries).to.deep.equal(["test/", "test/path2/", "test/path2/file1.txt", "test/path2/folder1/", "test/path2/folder1/file2.txt"]);
61+
});
62+
63+
it("zip.deleteFile() - delete folder", () => {
64+
const content = "test";
65+
const comment = "comment";
66+
const zip1 = new Zip({ noSort: true });
67+
zip1.addFile("test/");
68+
zip1.addFile("test/path1/");
69+
zip1.addFile("test/path1/file1.txt", content, comment);
70+
zip1.addFile("test/path1/folder1/");
71+
zip1.addFile("test/path1/folder1/file2.txt", content, comment);
72+
zip1.addFile("test/path2/");
73+
zip1.addFile("test/path2/file1.txt", content, comment);
74+
zip1.addFile("test/path2/folder1/");
75+
zip1.addFile("test/path2/folder1/file2.txt", content, comment);
76+
77+
zip1.deleteFile("test/path1/", false);
78+
79+
const zipEntries = zip1.getEntries().map((child) => child.entryName);
80+
81+
expect(zipEntries).to.deep.equal([
82+
"test/",
83+
"test/path1/file1.txt",
84+
"test/path1/folder1/",
85+
"test/path1/folder1/file2.txt",
86+
"test/path2/",
87+
"test/path2/file1.txt",
88+
"test/path2/folder1/",
89+
"test/path2/folder1/file2.txt"
90+
]);
91+
});
92+
93+
it("zip.deleteFile() - delete files", () => {
94+
const content = "test";
95+
const comment = "comment";
96+
const zip1 = new Zip({ noSort: true });
97+
zip1.addFile("test/");
98+
zip1.addFile("test/path1/");
99+
zip1.addFile("test/path1/file1.txt", content, comment);
100+
zip1.addFile("test/path1/folder1/");
101+
zip1.addFile("test/path1/folder1/file2.txt", content, comment);
102+
103+
zip1.deleteFile("test/path1/file1.txt", false);
104+
zip1.deleteFile("test/path1/folder1/file2.txt", false);
105+
106+
const zipEntries = zip1.getEntries().map((child) => child.entryName);
107+
108+
expect(zipEntries).to.deep.equal(["test/", "test/path1/", "test/path1/folder1/"]);
109+
});
110+
});
111+
31112
describe(".extractAllTo() - sync", () => {
32113
const ultrazip = [
33114
"./attributes_test/asd/New Text Document.txt",

zipFile.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,14 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
205205
* @param entryName
206206
* @returns {void}
207207
*/
208-
deleteFile: function (/*String*/ entryName) {
208+
deleteFile: function (/*String*/ entryName, withsubfolders = true) {
209209
if (!loadedEntries) {
210210
readEntries();
211211
}
212-
var entry = entryTable[entryName];
213-
if (entry && entry.isDirectory) {
214-
var _self = this;
215-
this.getEntryChildren(entry).forEach(function (child) {
216-
if (child.entryName !== entryName) {
217-
_self.deleteFile(child.entryName);
218-
}
219-
});
220-
}
221-
entryList.splice(entryList.indexOf(entry), 1);
222-
delete entryTable[entryName];
223-
mainHeader.totalEntries = entryList.length;
212+
const entry = entryTable[entryName];
213+
const list = this.getEntryChildren(entry, withsubfolders).map((child) => child.entryName);
214+
215+
list.forEach(this.deleteEntry);
224216
},
225217

226218
/**
@@ -234,9 +226,12 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
234226
readEntries();
235227
}
236228
const entry = entryTable[entryName];
237-
entryList.splice(entryList.indexOf(entry), 1);
238-
delete entryTable[entryName];
239-
mainHeader.totalEntries = entryList.length;
229+
const index = entryList.indexOf(entry);
230+
if (index >= 0) {
231+
entryList.splice(index, 1);
232+
delete entryTable[entryName];
233+
mainHeader.totalEntries = entryList.length;
234+
}
240235
},
241236

242237
/**
@@ -245,21 +240,24 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
245240
* @param entry
246241
* @return Array
247242
*/
248-
getEntryChildren: function (/*ZipEntry*/ entry) {
243+
getEntryChildren: function (/*ZipEntry*/ entry, subfolders = true) {
249244
if (!loadedEntries) {
250245
readEntries();
251246
}
252-
if (entry && entry.isDirectory) {
253-
const list = [];
254-
const name = entry.entryName;
255-
const len = name.length;
256-
257-
entryList.forEach(function (zipEntry) {
258-
if (zipEntry.entryName.startsWith(name)) {
259-
list.push(zipEntry);
247+
if (typeof entry === "object") {
248+
if (entry.isDirectory && subfolders) {
249+
const list = [];
250+
const name = entry.entryName;
251+
252+
for (const zipEntry of entryList) {
253+
if (zipEntry.entryName.startsWith(name)) {
254+
list.push(zipEntry);
255+
}
260256
}
261-
});
262-
return list;
257+
return list;
258+
} else {
259+
return [entry];
260+
}
263261
}
264262
return [];
265263
},

0 commit comments

Comments
 (0)