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.4k stars 510 forks source link

Issues with the ZipReaderStream example #509

Closed jespertheend closed 5 months ago

jespertheend commented 5 months ago

I wanted to take zip data from a POST request, decompress it and write it to disk. I tried to use this example as a starting point, but ended up running into a couple of issues. https://github.com/gildas-lormeau/zip.js/blob/1cb7354a27a3ad3cacc240d64d3e08ca15b7ee86/index.d.ts#L631-L641

  1. The first line gave me a type error, because new ZipWriterStream() is not a valid type for pipeThrough(). I think ZipWriterStream and ZipReaderStream need to be swapped in this example.
  2. The zip file I used contains directories, so Deno.create fails for any files inside subdirectories. A call to ensureFile from std/fs fixed this for me.
  3. I think it's best to await the entry.readable.pipeTo() call in case an error is thrown. I had wrapped my code with a try catch block but the errors from the second issue weren't caught.

This is what ended up working for me:

try {
    for await (const entry of (request.body.pipeThrough(new ZipReaderStream()))) {
        const fullPath = stdPath.resolve(tmpDir, entry.filename);
        if (entry.directory) {
            await ensureDir(fullPath);
            continue;
        }

        await ensureFile(fullPath);
        await entry.readable?.pipeTo((await Deno.create(fullPath)).writable);
    }
} catch (e) {
    console.error(e);
    throw new HttpError(STATUS_CODE.InternalServerError, "Failed to parse the zip file.");
}
gildas-lormeau commented 5 months ago

Thank you very much, I must admit I hadn't tested this piece of code. All your comments are pertinent. Would you like me to integrate the changes for you or would you prefer to submit a PR to make your contribution more official?

jespertheend commented 5 months ago

I can submit a PR!