Issues with the ZipReaderStream example
jespertheend opened this issue · 2 comments
jespertheend commented
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.
Lines 631 to 641 in 1cb7354
- The first line gave me a type error, because
new ZipWriterStream()
is not a valid type forpipeThrough()
. I thinkZipWriterStream
andZipReaderStream
need to be swapped in this example. - The zip file I used contains directories, so
Deno.create
fails for any files inside subdirectories. A call toensureFile
from std/fs fixed this for me. - I think it's best to
await
theentry.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
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
I can submit a PR!