Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.79k stars 1.3k forks source link

Read contents of a file within a zip #311

Open sethdorris opened 8 years ago

sethdorris commented 8 years ago

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

ckuhar-broadsoft commented 8 years ago

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) {

...
dduponchel commented 8 years ago

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 ?

KatherineWinter commented 4 years ago

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
}
tanuj1709 commented 4 years ago

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?

Mihai-github commented 1 year ago

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?