From 56f3aae2e5473728e58baa1cae5a88ceaab52ebf Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Sun, 16 Jun 2024 21:11:05 +0300 Subject: [PATCH 1/2] reset content after calling "compressToBuffer" --- zipFile.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zipFile.js b/zipFile.js index afd0787..354aa28 100644 --- a/zipFile.js +++ b/zipFile.js @@ -302,6 +302,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) { } mh.copy(outBuffer, dindex); + // Since we update entry and main header offsets, + // they are no longer valid and we have to reset content + // (Issue 64) + + inBuffer = outBuffer; + loadedEntries = false; + return outBuffer; }, @@ -371,6 +378,13 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) { mh.copy(outBuffer, dindex); // write main header + // Since we update entry and main header offsets, they are no + // longer valid and we have to reset content using our new buffer + // (Issue 64) + + inBuffer = outBuffer; + loadedEntries = false; + onSuccess(outBuffer); } }; From 37961981549326eeda082a5e75917fdc46eafb05 Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Sun, 16 Jun 2024 21:36:20 +0300 Subject: [PATCH 2/2] Add test for a issue --- test/mocha.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/mocha.js b/test/mocha.js index 9319061..de7785b 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -114,6 +114,21 @@ describe("adm-zip", () => { expect(zip1Entries).to.deep.equal(["windows/system32/drivers/etc/hosts.txt", "system32/drivers/etc/hosts.txt", "drivers/etc/hosts.txt", "hosts.txt"]); }); + // Issue 64 + it("zip.writeZip - multiple times", () => { + const zip = new Zip("./test/assets/ultra.zip"); + const fileName = pth.resolve(destination, "writezip"); + + for (let i = 0; i < 5; i++) zip.writeZip(`${fileName}.${i}.zip`); + + 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( + pth.normalize + ); + + const files = walk(destination); + expect(files.sort()).to.deep.equal(expected_list); + }); + /* it("repro: symlink", () => { const zip = new Zip("./test/assets/symlink.zip");