Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.76k stars 1.3k forks source link

wanna have streams & pipes #241

Open jimmywarting opened 8 years ago

jimmywarting commented 8 years ago

Quite important if we want to save large file. The point of this is that a browser can't hold so much RAM and I/We need a way to pipe it to the browsers Filesystem API

Now there is a browser stream api on the way and it would be really awesome if jszip could use this new cool api when it comes out.

In the meanwhile we could do something simple like the fetch API handles a stream read this: thats so fetch#streams It would look something like this:

var writer = get_a_filesystem_writer_from_filesystem_api()
writer.seek(0);

function pump(reader){
    reader.read().then(function(result){
        if(result.done) return;
        return new Promise(function(resolve, reject){
            writer.onwriteend = function(){
                resolve(pump(reader));
            }
            // result.data is a ArrayBuffer
            writer.write(result.data);
        });
    });
}

var stream = zip.generate({ type: "readableStream" });
pump( stream ).then(function(){
    console.log("done");
})

Or choose to implement the pollyfill if needed

I manage to request a really large file with Fetch API and piped it to the FileSystem API without the RAM ever going up so much. But then I just changed the 4 last lines with this:

fetch(url).then(r => {
    return pump(r.body.getReader())
}).then(function(){
    console.log("done");
})
jimmywarting commented 8 years ago

hmm, seams related to #121