Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addFile backslash test #500

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "";

Expand Down
1 change: 0 additions & 1 deletion test/large_directory_size/large_directory_size.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ describe("read zip file header with invalid large number of entries", () => {
}, new Error("Number of disk entries is too large"));
});
});

20 changes: 20 additions & 0 deletions test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down