Skip to content

Commit ba4e198

Browse files
authored
Merge pull request #507 from 5saviahv/issue-64
using writeZip() twice throws "Invalid LOC header (bad signature)" error
2 parents 50152d6 + 3796198 commit ba4e198

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

test/mocha.js

+15
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ describe("adm-zip", () => {
114114
expect(zip1Entries).to.deep.equal(["windows/system32/drivers/etc/hosts.txt", "system32/drivers/etc/hosts.txt", "drivers/etc/hosts.txt", "hosts.txt"]);
115115
});
116116

117+
// Issue 64
118+
it("zip.writeZip - multiple times", () => {
119+
const zip = new Zip("./test/assets/ultra.zip");
120+
const fileName = pth.resolve(destination, "writezip");
121+
122+
for (let i = 0; i < 5; i++) zip.writeZip(`${fileName}.${i}.zip`);
123+
124+
const expected_list = ["./test/xxx/writezip.0.zip", "./test/xxx/writezip.1.zip", "./test/xxx/writezip.2.zip", "./test/xxx/writezip.3.zip", "./test/xxx/writezip.4.zip"].map(
125+
pth.normalize
126+
);
127+
128+
const files = walk(destination);
129+
expect(files.sort()).to.deep.equal(expected_list);
130+
});
131+
117132
/*
118133
it("repro: symlink", () => {
119134
const zip = new Zip("./test/assets/symlink.zip");

zipFile.js

+14
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
302302
}
303303
mh.copy(outBuffer, dindex);
304304

305+
// Since we update entry and main header offsets,
306+
// they are no longer valid and we have to reset content
307+
// (Issue 64)
308+
309+
inBuffer = outBuffer;
310+
loadedEntries = false;
311+
305312
return outBuffer;
306313
},
307314

@@ -371,6 +378,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
371378

372379
mh.copy(outBuffer, dindex); // write main header
373380

381+
// Since we update entry and main header offsets, they are no
382+
// longer valid and we have to reset content using our new buffer
383+
// (Issue 64)
384+
385+
inBuffer = outBuffer;
386+
loadedEntries = false;
387+
374388
onSuccess(outBuffer);
375389
}
376390
};

0 commit comments

Comments
 (0)