From 4b05f6eaf6ecde9b8c0eec3edc5876916c6b1756 Mon Sep 17 00:00:00 2001 From: xjcb <> Date: Sun, 13 Oct 2024 18:59:10 +0200 Subject: [PATCH 1/3] Fix: don't override files with the same name --- harextract.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/harextract.html b/harextract.html index 6dfd27a..78b2650 100644 --- a/harextract.html +++ b/harextract.html @@ -281,6 +281,12 @@ let encoding = data[ent.id].response.content.encoding; if (encoding != 'base64') throw new Error(`Unsupported encoding for ${ent.name} (${ent.id}): ${encoding}`); + if (zip.file(filepath)) { + //if filename already exists, add a numbered suffix + const count = Number(filepath.match(/^.*_([0-9]*)\.[^\.]*$/)?.at(1)); + if (count) filepath = filepath.replace(/^(.*_)[0-9]*(\.[^\.]*)$/,`$1${count + 1}$2`); + else filepath = filepath.replace(/^(.*)(\.[^\.]*)$/,`$1_2$2`); + } zip.file(filepath, data[ent.id].response.content.text, { base64: true }); } catch (e) { console.log(e); From 0501398821cf8245ebcfe16d30301d62891c1aa2 Mon Sep 17 00:00:00 2001 From: chicheese <136516001+chicheese@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:35:22 -0700 Subject: [PATCH 2/3] Attempting xjcb-de's fix for multiple files with the same name Appending a numbered suffix on a file's name if that there are multiple files with the same name. --- harextract.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/harextract.html b/harextract.html index 78b2650..75bdf59 100644 --- a/harextract.html +++ b/harextract.html @@ -282,7 +282,7 @@ if (encoding != 'base64') throw new Error(`Unsupported encoding for ${ent.name} (${ent.id}): ${encoding}`); if (zip.file(filepath)) { - //if filename already exists, add a numbered suffix + //if filename already exists, add a numbered suffix. const count = Number(filepath.match(/^.*_([0-9]*)\.[^\.]*$/)?.at(1)); if (count) filepath = filepath.replace(/^(.*_)[0-9]*(\.[^\.]*)$/,`$1${count + 1}$2`); else filepath = filepath.replace(/^(.*)(\.[^\.]*)$/,`$1_2$2`); From e405276817e5e6eeb4fc254c63379cd8ff770718 Mon Sep 17 00:00:00 2001 From: chicheese <136516001+chicheese@users.noreply.github.com> Date: Wed, 22 Apr 2026 21:01:16 -0700 Subject: [PATCH 3/3] fix(zip): resolve duplicate filename collision: suffix only incremented once instead of looping The duplicate filename handling in buildZIP() only tried to rename a file once when it detected a collision. It would generate a candidate name like file_2.ext using a regex on the original filepath string, but it never checked if that candidate name was already taken in the zip before writing to it. This caused a bug where if 15+ files in the HAR shared the same path and name, only 2 files would end up in the zip: the original (file.ext) and one renamed copy (file_2.ext). Every file after that would just overwrite file_2.ext because the original filepath string never changed between loop iterations, so the regex always came up with the same _2 candidate. The fix was to replace the one-shot rename logic with a while loop that starts at counter 2 and keeps incrementing until it finds a candidate path that doesnt already exist in the zip. The filename and extension are split around the last dot (after the last slash, so dots in directory names dont cause issues). Files with and without extensions both work. Duplicates now each get their own file: file.ext, file_2.ext, file_3.ext, and so on for however many copies are in the HAR. --- harextract.html | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/harextract.html b/harextract.html index 75bdf59..ffdc5a6 100644 --- a/harextract.html +++ b/harextract.html @@ -282,10 +282,21 @@ if (encoding != 'base64') throw new Error(`Unsupported encoding for ${ent.name} (${ent.id}): ${encoding}`); if (zip.file(filepath)) { - //if filename already exists, add a numbered suffix. - const count = Number(filepath.match(/^.*_([0-9]*)\.[^\.]*$/)?.at(1)); - if (count) filepath = filepath.replace(/^(.*_)[0-9]*(\.[^\.]*)$/,`$1${count + 1}$2`); - else filepath = filepath.replace(/^(.*)(\.[^\.]*)$/,`$1_2$2`); + // file already exists in the zip so we need to find a unique name for it + // split the full path into the folder/directory part and the filename part + const slashIdx = filepath.lastIndexOf('/'); + const dir = slashIdx >= 0 ? filepath.substring(0, slashIdx + 1) : ''; + const filename = slashIdx >= 0 ? filepath.substring(slashIdx + 1) : filepath; + // split the filename into base and extension so we can stick the suffix in before the dot + // we use lastIndexOf on just the filename so dots in directory names dont mess things up + const dotIdx = filename.lastIndexOf('.'); + const base = dotIdx >= 0 ? filename.substring(0, dotIdx) : filename; + const ext = dotIdx >= 0 ? filename.substring(dotIdx) : ''; + // start at 2 since the original file already took the unsuffixed slot + let counter = 2; + // keep incrementing until we find a name thats not already in the zip + while (zip.file(`${dir}${base}_${counter}${ext}`)) counter++; + filepath = `${dir}${base}_${counter}${ext}`; } zip.file(filepath, data[ent.id].response.content.text, { base64: true }); } catch (e) {