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

Allow duplicate file names/entries when creating zip files? #37

Open
jimmywarting opened this issue Sep 11, 2019 · 1 comment
Open

Allow duplicate file names/entries when creating zip files? #37

jimmywarting opened this issue Sep 11, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@jimmywarting
Copy link
Contributor

As the Zip spec doesn’t forbid this case (I think), these Zip files are, in theory, okay. you might end up with multiple identical entries in the Zip, e.g. the exact same file name/path more than once in the same directory within the Zip. This is the case when multiple “from”'s are given which end up creating the same Filename at the same location within the resulting zip.

However, depending on how and where you try to extract them, you’ll end up with a whole slue or problems and errors.

however somewhere i think it might be handy when communicating between client and the server, if you know what you are doing, files can be generated from memory. Google drive for example allows many documents all named "Untitled Spreadsheet" or "Untitled Documents". As unique IDs were assigned to them on the server side, there was never a need to restrict the user like legacy file systems.

What would you say about loosing up this rule a bit and get rid of the duplicate rule, and put this responsibility upon the developer instead?

@jimmywarting jimmywarting changed the title Allow duplicate file names/entries? Allow duplicate file names/entries when creating zip files? Sep 11, 2019
@bencmbrook bencmbrook added the enhancement New feature or request label Aug 3, 2020
@Johnrobmiller
Copy link

Johnrobmiller commented May 16, 2024

Here's my solution:

Note that you won't be able to copy/paste this into your project because it has lines specific to my situation.

  const zip = new JSZip()

  const filenamesThusFar: string[] = []

  await Promise.all(
    downloads.map(async (download) => {
      if (!download.data) return

      try {
        // JSZip bug: JSZip will replace/overwrite files with the same name, and there is no option to override this.
        let name = download.name || v4()
        let nameCounter = 0
        filenamesThusFar.forEach((filename) => {
          if (filename === name) nameCounter++
        })
        filenamesThusFar.push(name)
        if (nameCounter) name = `${name}_${nameCounter}`
        const extension = getExtensionFromType(download.data.type)
        name = `${name}${extension || '.mp3'}`

        zip.file(name, download.data)

      } catch (error) {

        console.error(error)
        throw error
      }
    })
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants