Send zip created by node-zip

jszip, node.js

Solution

Using the archiver and string-stream packages:

var archiver = require('archiver')
var fs = require('fs')
var StringStream = require('string-stream')

http.createServer(function(request, response) {
  var dl = archiver('zip')
  dl.pipe(response)
  dl.append(new fs.createReadStream('/path/to/some/file.txt'), {name:'YoDog/SubFolder/static.txt'})
  dl.append(new StringStream("Ooh dynamic stuff!"), {name:'YoDog/dynamic.txt'})
  dl.finalize(function (err) {
    if (err) res.send(500)
  })
}).listen(3000)

Problem

Let's say you create a zip file in-memory following the example from `node-zip`'s documentation: ``` var zip = new require('node-zip')() zip.file('test.file', 'hello there') var data = zip.generate({type:'string'}) ``` How do you then send `data` to a browser such that it will accept it as a download? I tried this, but the download hangs at 150/150 bytes AND makes Chrome start eating 100% CPU: ``` res.setHeader('Content-type: application/zip') res.setHeader('Content-disposition', 'attachment; filename=Zippy.zip'); res.send(data) ``` So what's the proper way to send zip data to a browser?

Original source