Closed vi117 closed 10 months ago
Reproduce the problem with the following code.
Deno version is
deno 1.38.4 (release, x86_64-pc-windows-msvc) v8 12.0.267.1 typescript 5.2.2
The content of test.txt is "asdfz".
test.txt
import * as zip from "https://deno.land/x/zipjs@v2.7.31/index.js"; async function testZipInDeno(useWebWorkers: boolean) { const blobWriter = new zip.BlobWriter("application/zip"); const zipFile = new zip.ZipWriter(blobWriter, { bufferedWrite: true, dataDescriptor: false, }); const filepath = "test.txt"; const [file, stat] = await Promise.all([ await Deno.open(filepath, { read: true }), await Deno.stat(filepath)]); const reader = { readable: file.readable // .pipeThrough(new TransformStream({ // transform(chunk, controller) { // console.log("read", chunk.length, "bytes", chunk.byteLength, "buffer", chunk.buffer); // controller.enqueue(chunk); // } // })) , size: stat.size, }; await zipFile.add( filepath, reader, { useWebWorkers: useWebWorkers, useCompressionStream: true, } ); await zipFile.close(); const blob = await blobWriter.getData(); console.log(blob.size); } await testZipInDeno(true); await testZipInDeno(false);
The result is
It's the same problem as issue #453.
If you uncomment it and run it, it will output something like this:
read 5 bytes 5 buffer ArrayBuffer { [Uint8Contents]: <61 73 64 66 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 65436 more bytes>, byteLength: 65536 } 297 read 5 bytes 5 buffer ArrayBuffer { [Uint8Contents]: <61 73 64 66 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 65436 more bytes>, byteLength: 65536 } 217
https://github.com/gildas-lormeau/zip.js/blob/e0a277fc2dfe469f50e86b55101816a235d38b1a/lib/core/codec-worker.js#L237-L274
The problem is caused at line 242 message.value = value.buffer; The size of 'Uint8Array' and 'ArrayBuffer' of 'Uint8Array.buffer' may differ when reading files from Deno.
message.value = value.buffer;
The simple solution I think is the following code.
if (value.byteLength < value.buffer.byteLength) { message.value = value.buffer.slice(0, chunk.byteLength); } else { message.value = value.buffer; }
Thanks for the work you do.
Thank you very much for the detailed bug report and the fix proposal. I've just published the version 2.7.32 which includes the fix.
Reproduce
Reproduce the problem with the following code.
Deno version is
The content of
test.txt
is "asdfz".The result is
It's the same problem as issue #453.
If you uncomment it and run it, it will output something like this:
Cause
https://github.com/gildas-lormeau/zip.js/blob/e0a277fc2dfe469f50e86b55101816a235d38b1a/lib/core/codec-worker.js#L237-L274
The problem is caused at line 242
message.value = value.buffer;
The size of 'Uint8Array' and 'ArrayBuffer' of 'Uint8Array.buffer' may differ when reading files from Deno.The simple solution I think is the following code.
Thanks for the work you do.