gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.42k stars 512 forks source link

using BlobWriter throws "TypeError: t is not a constructor TypeError: t is not a constructor" Angular 11 #314

Closed eafiontzi closed 2 years ago

eafiontzi commented 2 years ago

Hello all,

I have been trying to use zip.js in an Angular 11 project. To import the library I used this comment with NPM

const zip = require("../../../../../node_modules/@zip.js/zip.js/dist/zip.min.js");
zip.configure({useWebWorkers: false});

I have been able to get all file entries using BlobReader and ZipReader

const reader = new zip.ZipReader(new zip.BlobReader(file));
reader.getEntries().then((entries: FileEntry[]) => {
  console.log(entries)
});

But when I try getting the actual data from one entry using the code from the ZipReader example (using either TextWriter or BlobWriter)

  // get first entry content as text by using a TextWriter
  const text = await entries[0].getData(
    // writer
    new zip.TextWriter(),
    // options
    { 
      onprogress: (index, max) => {
         // onprogress callback
      }
    }
  );
  // text contains the entry data as a String
  console.log(text);

I get the error

core.js:6150 ERROR Error: Uncaught (in promise): TypeError: t is not a constructor
TypeError: t is not a constructor
    at new ut (zip.min.js:1:11887)
    at zip.min.js:1:14882
    at zip.min.js:1:14882
    at yt (zip.min.js:1:15211)
    at kt (zip.min.js:1:15457)
    at ze.getData (zip.min.js:1:23098)
    at resolvePromise (zone-evergreen.js:798:1)
    at zone-evergreen.js:705:1
    at rejected (tslib.es6.js:72:88)
    at ZoneDelegate.invoke (zone-evergreen.js:364:1)
    at Object.onInvoke (core.js:28528:1)
    at ZoneDelegate.invoke (zone-evergreen.js:363:1)
    at Zone.run (zone-evergreen.js:123:1)
    at zone-evergreen.js:857:1
    at ZoneDelegate.invokeTask (zone-evergreen.js:399:1)
    at Object.onInvokeTask (core.js:28515:1)

I have seen this issue and actually console.log(zip) right after my import statement. I get the same object for BlobReader and BlobWriter, only BlobReader seems to work while BlobWriter not

image

gildas-lormeau commented 2 years ago

Are you using the latest version of zip.js? The stack trace does not seem to correspond to zip.min.jsas it exists today. Alternatively and maybe more conveniently, what error is displayed if you replace require("../../../../../node_modules/@zip.js/zip.js/dist/zip.min.js"); with require("../../../../../node_modules/@zip.js/zip.js/dist/zip.js");?

eafiontzi commented 2 years ago

Hello, thank you very much for you answer,

The path you suggested gives the following error:

image

I have tried with

require("../../../../../node_modules/@zip.js/zip.js/dist/zip-full.js");

and it seems to be working. I will investigate more and if everything is in place I will close the issue.

gildas-lormeau commented 2 years ago

Sorry, I didn't notice you disabled web workers support (see {useWebWorkers: false} in your code). That's why it does not work with zip.js and zip.min.js but it does work with zip-full.js (see https://github.com/gildas-lormeau/zip.js/blob/master/dist/README.md). You should also be able to use zip-no-worker.min.js which is a bit ligther.