jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Support adding files with web streams as well

Open jimmywarting opened this issue 8 years ago • 5 comments

zip = new jszip()
stream = new ReadableStream({...})
zip.file('file1.txt', stream)

fetch(url).then(res => {
    // Same as above but another way to get a readable stream
    zip.file('file.txt', res.body)
})

// Same as above but another way to turn a blob into a readable stream
zip.file('file.txt', (new Response(blob)).body)

so the supported data could be: String/ArrayBuffer/Uint8Array/Buffer/Blob/Promise/Nodejs stream/Web stream

jimmywarting avatar Aug 25 '16 11:08 jimmywarting

Good idea :+1: It may need some work to map the .pause() and .resume() and wire the backpressure mechanism.

Before doing the same thing on the generate side, I'd wait for the stability warning to be removed from the specs:

The writable stream spec is still stabilizing, although it has recently improved a lot and is more aligned with our vision. Still, it is not quite yet ready to ship.

dduponchel avatar Aug 27 '16 20:08 dduponchel

The good thing about adding web stream to the generate side as well is that it can be piped to my StreamSaver lib seamlessly that are using web streams already 👍

So instead of doing this (jsfiddle)

let writeStream = streamSaver.createWriteStream('output.zip').getWriter()

zip.generateInternalStream({type: "uint8array"})
.on('data', data => writeStream.write(data))
.on('error', err => console.error(err))
.on('end', () => writeStream.close())
.resume()

You could then actually just do something like:

let fileStream = streamSaver.createWriteStream('output.zip')
zip.generateReadableStream().pipeTo(fileStream)

jimmywarting avatar Aug 27 '16 20:08 jimmywarting

What's the status on this issue?

Nnoerregaard avatar May 07 '19 07:05 Nnoerregaard

The status is that the owner don't seem to have time to work on jszip.

I would be willing to help with PR, but they don't get merged or reviewed. I'm also not keen to work on such a big backward compatible lib that must support very old browsers and node versions...

I almost think this issue should be closed in favor of #346 so we can support both whatwg and node streams. at least to a beginning.

jimmywarting avatar May 07 '19 08:05 jimmywarting

bump

alex737876 avatar Sep 09 '23 08:09 alex737876