diff --git a/adm-zip.js b/adm-zip.js index 33316f6..51e37d7 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -423,13 +423,14 @@ module.exports = function (/**String*/ input, /** object */ options) { * @param {number | object} attr - number as unix file permissions, object as filesystem Stats object */ addFile: function (/**String*/ entryName, /**Buffer*/ content, /**String*/ comment, /**Number*/ attr) { + entryName = Utils.canonical(entryName); let entry = getEntry(entryName); const update = entry != null; // prepare new entry if (!update) { entry = new ZipEntry(); - entry.entryName = Utils.canonical(entryName); + entry.entryName = entryName; } entry.comment = comment || ""; diff --git a/test/large_directory_size/large_directory_size.test.js b/test/large_directory_size/large_directory_size.test.js index 571659d..1d896e2 100644 --- a/test/large_directory_size/large_directory_size.test.js +++ b/test/large_directory_size/large_directory_size.test.js @@ -14,4 +14,3 @@ describe("read zip file header with invalid large number of entries", () => { }, new Error("Number of disk entries is too large")); }); }); - diff --git a/test/mocha.js b/test/mocha.js index cbc481f..9b816b1 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -255,6 +255,26 @@ describe("adm-zip", () => { expect(zip2Entries).to.deep.equal(["c.txt", "b.txt", "a.txt"]); }); + it("windows style path with backslash should be converted to slashes", () => { + const content = "test"; + const comment = "comment"; + + // is sorting working - value "false" + const zip1 = new Zip({ noSort: true }); + // next 3 lines are with identical names, so only one file is added + zip1.addFile("..\\..\\..\\windows\\system32\\drivers\\etc\\hosts.txt", content, comment); + zip1.addFile("aa\\bb\\..\\cc\\..\\..\\windows\\system32\\drivers\\admin\\..\\etc\\hosts.txt", content, comment); + zip1.addFile(".\\windows\\system32\\drivers\\etc\\hosts.txt", content, comment); + // 3 other file + zip1.addFile("system32\\drivers\\etc\\hosts.txt", content, comment); + zip1.addFile("drivers\\etc\\hosts.txt", content, comment); + zip1.addFile(".\\hosts.txt", content, comment); + zip1.toBuffer(); + + const zip1Entries = zip1.getEntries().map((e) => e.entryName); + expect(zip1Entries).to.deep.equal(["windows/system32/drivers/etc/hosts.txt", "system32/drivers/etc/hosts.txt", "drivers/etc/hosts.txt", "hosts.txt"]); + }); + /* it("repro: symlink", () => { const zip = new Zip("./test/assets/symlink.zip");