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

Possible to get a WritableStream handle to add file? #411

Closed gizahNL closed 1 year ago

gizahNL commented 1 year ago

Is it possible to create a WritableStream when adding a file?

I have a ton of ReadableStreams (from response.body) that need to be concatted into a single file that needs to be made part of a zip. Having a WritableStream would allow me to simply pipe my readable streams into the file.

0f-0b commented 1 year ago

You can get a pair of readable and writable streams from a transform stream.

const { readable, writable } = new TransformStream();
(async () => {
  for (const body of responseBodies) {
    await body.pipeTo(writable, { preventClose: true });
  }
  await writable.close();
})();
await zipWriter.add("concatenated_files", { readable });

Another example from the spec.

gizahNL commented 1 year ago

Thanks, I think that should work :)

gildas-lormeau commented 1 year ago

Note that you should be able to pass also an Array of ReadableStream (or Reader) instances to ZipWriter#add(). See below for example.

https://github.com/gildas-lormeau/zip.js/blob/151b5984728dddad42f2f87da39b1efc9b9b7519/tests/all/test-split-data.js#L23-L27