node-zip-stream icon indicating copy to clipboard operation
node-zip-stream copied to clipboard

How can I deal with different files with the same name and zip them?

Open PseudoCowboy opened this issue 5 years ago • 1 comments

I have three jpg files, and they have same name. I deal with them like this: archive.entry(filestream, { name: ${folderName + item.info.name} }, (err, entry) => { ... } And the result is, I get a zip file, unarchive it, there is only one jpg. Is there any options to avoid this?

BTW, the api documentation page is 404.

PseudoCowboy avatar Sep 10 '20 07:09 PseudoCowboy

I faced a similar issue, and to avoid file name collisions I maintained a map(name, occurence) to keep a track of all file names If I found a duplicate, I added a suffix 1, 2, 3 ...

function foo(folderName: string, items: Record<string, any>[], archive: any, filestream: any): void {
  const fileNameMap = new Map<string, number>()

  for(const item of items) {
    const fileName = folderName + item.info.name


    const occurenceTillNow = fileNameMap.get(fileName) ?? 0
    fileNameMap.set(fileName, occurenceTillNow+1)

    const suffix = occurenceTillNow===0 ? "" : `${occurenceTillNow}`
    archive.entry(filestream, { name: `${fileName}${suffix}`} )
  }
}

97amarnathk avatar Mar 05 '22 10:03 97amarnathk