Dynamically create and stream zip to client

后端 未结 4 1312
滥情空心
滥情空心 2020-12-02 11:36

I am using NodeJs (w/express) and I am trying to stream a zip file back to the client. The files contained in the zip do not live on the file system, rather they are create

4条回答
  •  我在风中等你
    2020-12-02 12:03

    Yes, it's possible. I recommend taking a look at Streams Playground to get a feel for how Node Streams work.

    The zip compression in the core zlib library doesn't seem to support multiple files. If you want to go with tar-gzip, you could tar it with node-tar. But if you want ZIP, adm-zip looks like the best option. Another possibility is node-archiver.

    Update:

    This example shows how to use Archiver, which supports streams. Just substitute fs.createReadStream with the streams you're creating dynamically, and have output stream to Express's res rather than to fs.createWriteStream.

    var fs = require('fs');
    
    var archiver = require('archiver');
    
    var output = fs.createWriteStream(__dirname + '/example-output.zip');
    var archive = archiver('zip');
    
    output.on('close', function() {
      console.log('archiver has been finalized and the output file descriptor has closed.');
    });
    
    archive.on('error', function(err) {
      throw err;
    });
    
    archive.pipe(output);
    
    var file1 = __dirname + '/fixtures/file1.txt';
    var file2 = __dirname + '/fixtures/file2.txt';
    
    archive
      .append(fs.createReadStream(file1), { name: 'file1.txt' })
      .append(fs.createReadStream(file2), { name: 'file2.txt' });
    
    archive.finalize(function(err, bytes) {
      if (err) {
        throw err;
      }
    
      console.log(bytes + ' total bytes');
    });
    

提交回复
热议问题