node-archiver: Streaming to res not working, getting empty errored file

Hello, I run in quite the same issue (when streaming to file it works, when streaming to http client it gives corrupt files.), #161 seems to have same issues, but i’m runing on local with nodejs and nodemon, on windows. version of node is 4.2.1, archiver 0.17.0 here is my code. the res ‘end’ event is not raised. Nor is the res ‘data’ event.

    var archive = archiver('zip');
    archive.on('error', function(err) {
        console.error(err);
        res.status(500).send({error: err.message});
    });

    //on stream closed we can end the request
    res.on('close', function() {
        console.log('Archive wrote %d bytes', archive.pointer());
        return res.status(200).send('OK').end();
    });

    res.attachment('myzip.zip');

    //this is the streaming magic
    archive.pipe(res);

    archive.append('hello zip1',{name : 'text1.txt'});
    archive.append('hellooooo zip2',{ name : 'test2.txt'});

            archive.finalize(function(err, bytes) {
                if (err) {
                    throw err;
                }
                console.log(bytes + ' total bytes');
            });
    );

here is what i get as a file : image

the example i’m inspired by is this one : http://stackoverflow.com/questions/20107303/dynamically-create-and-stream-zip-to-client/25210806#25210806

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

This is the fix: app.use(require(‘connect-livereload’)({ ignore: [‘download’] }));

By using on('end') event. See this example with expressjs. Le dim. 29 nov. 2015 à 00:35, Sky Lightna notifications@github.com a écrit :

@ctalkington https://github.com/ctalkington I apologize in advance since I only picked up node earlier this week, but how will I know that finalize has finished executing without the use of a callback?

— Reply to this email directly or view it on GitHub https://github.com/archiverjs/node-archiver/issues/170#issuecomment-160345754 .