jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Read contents of a file within a zip

Open sethdorris opened this issue 9 years ago • 5 comments

I cannot find documentation on this .. is there a good example on how to do this?

sethdorris avatar Jun 24 '16 22:06 sethdorris

I used forEach(), I'd like to know a better way.

 configZip.loadAsync(rawConfigZipBuffer)
            .then(function(configZip) {
                //
                // generate a table of contents
                //

                configZip.forEach(function(filePath, file) {

...

ckuhar-broadsoft avatar Jun 30 '16 10:06 ckuhar-broadsoft

A bit late: promises are chainable, if you return a promise in a then, its result will be available in the next then. See the upgrade guide for an other example (look for Promises are chainable).

JSZip.loadAsync(binaryContent).then(function (zip) {
  return zip.file("path/to/file.txt").async("text");
}).then(function (txt) {
  console.log("the content is", txt);
});

@sethdorris does this solve your issue ?

dduponchel avatar Jul 19 '16 17:07 dduponchel

If you want to read all the contents in a zip, and return it all at once as an object array with the file name.. You can do:

export async function getZipFilesContent (data) {
  const zipContent = []
  const promises = []
  const zip = (await JSZip.loadAsync(data))
  zip.forEach(async (relativePath, file) => {
    const promise = file.async('string')
    promises.push(promise)
    zipContent.push({
      file: relativePath,
      content: await promise
    })
  })

  await Promise.all(promises)
  return zipContent
}

KatherineWinter avatar Jul 11 '20 15:07 KatherineWinter

If you want to read all the contents in a zip, and return it all at once as an object array with the file name.. You can do:

export async function getZipFilesContent (data) {
  const zipContent = []
  const promises = []
  const zip = (await JSZip.loadAsync(data))
  zip.forEach(async (relativePath, file) => {
    const promise = file.async('string')
    promises.push(promise)
    zipContent.push({
      file: relativePath,
      content: await promise
    })
  })

  await Promise.all(promises)
  return zipContent
}

can you give your demo project?

tanuj1709 avatar Jul 31 '20 06:07 tanuj1709

Hey did anyone have an issue with 2 same zip files where one has inside the zipContents.files a structure and the other one which holds the same content differently?

Mihai-github avatar Sep 16 '23 21:09 Mihai-github