node-archiver icon indicating copy to clipboard operation
node-archiver copied to clipboard

appending many files to archive

Open rvwhitney opened this issue 4 years ago • 2 comments

Greetings! Hope you're having a great day! I am almost there for zipping up many files (buffers) for downloading Here is my code:

socket.on('zip-selected', function(data){
            if(data[1]){
                //~ var files = [];
                // create a file to stream archive data to.
                const output            = fs.createWriteStream(__dirname + '/files/' + data[1] + '.zip');
                const archive           = archiver('zip', {
                    //zlib: { level: 9 } // Sets the compression level.
                    store: true
                });
                // good practice to catch warnings (ie stat failures and other non-blocking errors)
                archive.on('warning', function(err) {
                    if (err.code === 'ENOENT') {
                    // log warning
                    } else {
                    // throw error
                        throw err;
                    }
                });

                // good practice to catch this error explicitly
                archive.on('error', function(err) {
                    throw err;
                });
                var ids = data[0], buffer3;

                log(__line__, 'IDS', ids);
                //~ return;
                db.rows('downloadZip', currSession, ids, function(err, rows){
                    if (err) {
                        socket.emit('err', 'Error is: ' + err );
                        return;
                    } else {
                        for(var i = 0;i < rows.length;i++){
                            log(__line__, 'ZIPPING FILES', rows[i].NAME);
                            buffer3 = Buffer.from(rows[i].FILE);
                            archive.append(buffer3, { name: rows[i].NAME });
                        }
                        archive.pipe(output);
                        archive.finalize();
                    }
                });
            }
    });

what I am getting is the first file only in the archive - and it is intact on extracting The code assumes multiple files - ids contains [1,2,3, ...] so I want to have 2 or more files in the archive does anything jump out at you that is wrong here?
Anything else I can share?

Thank you!

rvwhitney avatar May 10 '21 22:05 rvwhitney

data[0] needed to be just data - I was getting only the first element in the array

rvwhitney avatar May 11 '21 12:05 rvwhitney

With that problem solved, I need a callback function when all files have been written to the archive before offering for download

output.on('end', function() {
      console.log('done');
      socket.emit('zip-selected', file);
});

does not seem to do anything

rvwhitney avatar May 11 '21 17:05 rvwhitney