thejoshwolfe / yauzl

yet another unzip library for node
MIT License
737 stars 80 forks source link

Error while opening file under mocha test #105

Closed brianignacio5 closed 5 years ago

brianignacio5 commented 5 years ago

I have implemented a yauzl.open(path, { lazyEntries: true}, (error, zipfile) => {}); that works fine for normal operation.

The failure happens when I try to do the same inside a mocha test, where the file gives an error when trying to open the zip file as shown here:

Error: end of central directory record signature not found
    at /myproject/node_modules/yauzl/index.js:187:14
    at /myproject/node_modules/yauzl/index.js:631:5
    at /myproject/node_modules/fd-slicer/index.js:32:7
    at FSReqWrap.wrapper [as oncomplete] (fs.js:658:17)

After running the test, the zip file is corrupted and if you try to extract it, you get a .zip.cpgz (Im using MacOS Mojave)

Note that is the same file that works when running from the main code. The mocha test use a tdd style. For context, Im using the VScode extension test test runner since I am working on a extension.

If there is anything I can add please let me know.

thejoshwolfe commented 5 years ago

The most likely cause of corrupted zipfile bugs is the asynchronous operation that writes the zipfile not finishing before you try to read the file. Is there a setup step in your test system that creates the file or copies it from somewhere?

I've often seen people watch for a stream 'end' event instead of a stream 'finish' (posix) or 'close' (windows) event. Could that be the problem here?

brianignacio5 commented 5 years ago

In the setup phase I would open an existing file, no creation or copy from somewhere.

I'm using yauzl in a Promise which resolves on the zip file end event.

Update: Tried the combination of using 'finish' or 'close' but the problem still continues. If I'll try to debug it the events are not even reached.

thejoshwolfe commented 5 years ago

I'm using yauzl in a Promise which resolves on the end event, I will try with both finish and close and see how it goes.

I don't follow this. The error you're getting can only come from the yauzl.open() call, so the 'end' event or any Promise pattern shouldn't matter. The 'finish' and 'close' events I'm referring to are from a fs.createWriteStream() stream, not from yauzl.

brianignacio5 commented 5 years ago

Yes, sorry I was not clear. So 'end' event I am referring to is the one from zip file. Will update on first post. In the write stream close event Im doing the same as Cpp tools extension here : https://github.com/Microsoft/vscode-cpptools/blob/master/Extension/src/packageManager.ts#L376

brianignacio5 commented 5 years ago

The issue had nothing to do with yauzl, another test was breaking the file. Sorry for the inconvenience.