Archiver
A streaming interface for archive generation
Visit the API documentation for a list of all methods available.
Install
npm install archiver --save
Quick Start
const fs = require('fs');
const archiver = require('archiver');
const output = fs.createWriteStream(__dirname + '/example.zip');
const archive = archiver('zip', {
zlib: { level: 9 }
});
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
output.on('end', function() {
console.log('Data has been drained');
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
} else {
throw err;
}
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
const file1 = __dirname + '/file1.txt';
archive.append(fs.createReadStream(file1), { name: 'file1.txt' });
archive.append('string cheese!', { name: 'file2.txt' });
const buffer3 = Buffer.from('buff it!');
archive.append(buffer3, { name: 'file3.txt' });
archive.file('file1.txt', { name: 'file4.txt' });
archive.directory('subdir/', 'new-subdir');
archive.directory('subdir/', false);
archive.glob('file*.txt', {cwd:__dirname});
archive.finalize();
Formats
Archiver ships with out of the box support for TAR and ZIP archives.
You can register additional formats with registerFormat
.
You can check if format already exists before to register a new one with isRegisteredFormat
.