Jszip: read the content of a zip file

Created on 20 Sep 2016  路  1Comment  路  Source: Stuk/jszip

Hi,
I use this code to download a zip from the web and read that, but I do not know what I have to do after that:

$http.post(url, data, {responseType: 'arraybuffer'})
    .then(function successCallback(file_response) {
      var file = new Blob([file_response.data], {type: 'application/zip'});
      var zip = new JSZip();
      zip.loadAsync(file)
        .then(function (content) {
            // use content
        });
    }, function errorCallback(file_error) {
    });

What can I do with "content"? I need to open it and read the content of the files (actually I have always one file). How can I do that?

Thanks.

questiosupport

Most helpful comment

content is actually the up-to-date zip object: you use file(name) to get the entry then async to get the full content for example.

zip.loadAsync(file).then(function (content) {
  zip.file("my_file.png").async("uint8array").then(...);
})

If you only have one file, you can also chain promises directly:

JSZip.loadAsync(file_response.data).then(function (zip) {
  return zip.file("my_file.png").async("uint8array");
}).then(function (pngContent) {
  // use the uint8array here
})

>All comments

content is actually the up-to-date zip object: you use file(name) to get the entry then async to get the full content for example.

zip.loadAsync(file).then(function (content) {
  zip.file("my_file.png").async("uint8array").then(...);
})

If you only have one file, you can also chain promises directly:

JSZip.loadAsync(file_response.data).then(function (zip) {
  return zip.file("my_file.png").async("uint8array");
}).then(function (pngContent) {
  // use the uint8array here
})
Was this page helpful?
0 / 5 - 0 ratings