
Zip data from buffer without writing to filesystem with the Node.js module archiver. All the examples offered in the description of Archiver NPM Module explains how to create a Zip writing to a file. But if you need to write to a stream and return that stream as a buffer, here is an example.
Util function
import archiver from 'archiver'
import { Writable } from 'stream'
/**
* @param {array<{data: Buffer, name: String}>} files
* @returns {Promise<Buffer>}
*/
function zipFiles (files) {
return new Promise((resolve, reject) => {
const buffs = []
const converter = new Writable()
converter._write = (chunk, encoding, cb) => {
buffs.push(chunk)
process.nextTick(cb)
}
converter.on('finish', () => {
resolve(Buffer.concat(buffs))
})
const archive = archiver('zip', {
zlib: { level: 9 }
})
archive.on('error', err => {
reject(err)
})
archive.pipe(converter)
for (const file of files) {
archive.append(file.data, { name: file.name })
}
archive.finalize()
})
}
Usage
const files = [
...
{
data: Buffer.from('just a buf'),
name: 'file.txt'
},
...
]
const zipBuffer = zipFiles(files)