express-zip icon indicating copy to clipboard operation
express-zip copied to clipboard

Write Stream into res.zip

Open CrackerakiUA opened this issue 9 years ago • 5 comments

This module looks so cool. Can you check my issue http://stackoverflow.com/questions/34168669/windows-azure-storage-blobs-to-zip-file-with-express ? Basically i am trying to read image from database via stream, and at the moment i am saving this stream in disk, then use res.zip. Is it possible to use directly stream to res.zip?

CrackerakiUA avatar Dec 09 '15 17:12 CrackerakiUA

Hi CrackerakiUA. It looks like you found the other issue where someone else suggested how to do this. You can see in this line of code that under the covers you can in fact pass a readStream directly:

https://github.com/thrackle/express-zip/blob/master/lib/express-zip.js#L55

So you are basically doing something like:

var zip = zipstream(exports.options);
zip.pipe(express.response || http.ServerResponse.prototype); // res is a writable stream

var addFile = function(file, cb) {
  zip.entry(fs.createReadStream(file.path), { name: file.name }, cb);
};

async.forEachSeries(files, addFile, function(err) {
  if (err) return cb(err);
  zip.finalize(function(bytesZipped) {
    cb(null, bytesZipped);
  });
});

Apologize if I've made horrible errors above; I haven't been on this for a bit.

thrackle avatar Dec 09 '15 18:12 thrackle

FWIW I've gone ahead and blocked off some time this Sunday for me to go through and get this added to the API, as well as close all the other PRs / issues. Glad this is helpful!

thrackle avatar Dec 09 '15 18:12 thrackle

Generally as i see we need to modify the plugin, which will create array of streams which user can use.

CrackerakiUA avatar Dec 09 '15 18:12 CrackerakiUA

I'll add support this weekend.

On Wed, Dec 9, 2015 at 11:53 AM, Honchar Denys [email protected] wrote:

Generally as i see we need to modify the plugin, which will create array of streams which user can use.

— Reply to this email directly or view it on GitHub https://github.com/thrackle/express-zip/issues/9#issuecomment-163356203.

thrackle avatar Dec 09 '15 18:12 thrackle

i will fork and try to add it.

CrackerakiUA avatar Dec 09 '15 19:12 CrackerakiUA