Node.js archiver has fuzzy behavior

I am using the following code to zip an entire folder consisting of only .txt files and send it to the client as a .zip file.

var time = new Date().getTime();
var userId = parseInt(req.params.userId);
console.log("User ID: "+userId);
var output = fs.createWriteStream('user_archive/temp-'+userId+'.zip');
var arch = archiver('zip');

output.on('close', function () {
    console.log(arch.pointer() + ' total bytes\n\r__');

    res.header('Content-disposition', 'attachment; filename=archive-'+time+'.zip');
    res.header('Content-type', 'application/octet-stream');
    var filestream = fs.createReadStream('user_archive/temp-'+userId+'.zip');
    filestream.pipe(res);

});

arch.on('error', function(err){
    throw err;
});

arch.pipe(output);
arch.bulk([
    { expand: true, cwd: 'user_archive/temp-'+userId+'/', src: ['*.txt'], dest: 'messages-'+time}
]).finalize();

      

The problem I'm running into is that I don't get the same number of bytes when testing this code, but console.log(arch.pointer() + ' total bytes\n\r__');

gives me values โ€‹โ€‹of 22 bytes that correspond to an invalid archive, the correct number of bytes. Another thing is that after 5-6 consecutive tests I get the correct archive until I stop and restart node. Then it happens again: 5 invalid archives followed by the correct archives. My question is, why is this fuzzy behavior?

+3


source to share





All Articles