jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Reading blob on NodeJs fails

Open Michael-1 opened this issue 1 year ago • 2 comments

I’m executing the example code for get a file with an Ajax call on NodeJS 18:

"use strict";

fetch("/jszip/test/ref/text.zip")       // 1) fetch the url
    .then(function (response) {                       // 2) filter on 200 OK
        if (response.status === 200 || response.status === 0) {
            return Promise.resolve(response.blob());
        } else {
            return Promise.reject(new Error(response.statusText));
        }
    })
    .then(JSZip.loadAsync)                            // 3) chain with the zip promise
    .then(function (zip) {
        return zip.file("Hello.txt").async("string"); // 4) chain with the text content promise
    })
    .then(function success(text) {                    // 5) display the result
        $("#fetch").append($("<p>", {
            "class": "alert alert-success",
            text: "loaded, content = " + text
        }));
    }, function error(e) {
        $("#fetch").append($("<p>", {
            "class": "alert alert-danger",
            text: e
        }));
    });

The code fails:

[email protected]/node_modules/jszip/lib/utils.js:479
                new Error("Can't read the data of '" + name + "'. Is it " +
                ^
Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?

Michael-1 avatar Jun 01 '23 09:06 Michael-1

Using response.arrayBuffer() instead of response.blob() makes it work.

Michael-1 avatar Jun 01 '23 11:06 Michael-1

I found this bug today. Here is a shorter reproducing sample:

npm i jszip
echo 'Hello World' > hello.txt && zip hello.zip hello.txt
node -e "require('jszip').loadAsync(new Blob([require('fs').readFileSync('hello.zip')]));"
/home/ulysses/temp/jszip-test/node_modules/jszip/lib/utils.js:479
                new Error("Can't read the data of '" + name + "'. Is it " +
                ^

Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?
    at /home/ulysses/temp/jszip-test/node_modules/jszip/lib/utils.js:479:17

Node.js v18.16.0

Version info:

  • Zip: 3.0
  • Node.js: 18.16.0
  • jszip: 3.10.1

UlyssesZh avatar Jul 26 '23 03:07 UlyssesZh