Archiver

a streaming interface for archive generation

README

Archiver


A streaming interface for archive generation

Visit the API documentation for a list of all methods available.

Install


  1. ``` sh
  2. npm install archiver --save
  3. ```

Quick Start


  1. ``` js
  2. // require modules
  3. const fs = require('fs');
  4. const archiver = require('archiver');

  5. // create a file to stream archive data to.
  6. const output = fs.createWriteStream(__dirname + '/example.zip');
  7. const archive = archiver('zip', {
  8.   zlib: { level: 9 } // Sets the compression level.
  9. });

  10. // listen for all archive data to be written
  11. // 'close' event is fired only when a file descriptor is involved
  12. output.on('close', function() {
  13.   console.log(archive.pointer() + ' total bytes');
  14.   console.log('archiver has been finalized and the output file descriptor has closed.');
  15. });

  16. // This event is fired when the data source is drained no matter what was the data source.
  17. // It is not part of this library but rather from the NodeJS Stream API.
  18. // @see: https://nodejs.org/api/stream.html#stream_event_end
  19. output.on('end', function() {
  20.   console.log('Data has been drained');
  21. });

  22. // good practice to catch warnings (ie stat failures and other non-blocking errors)
  23. archive.on('warning', function(err) {
  24.   if (err.code === 'ENOENT') {
  25.     // log warning
  26.   } else {
  27.     // throw error
  28.     throw err;
  29.   }
  30. });

  31. // good practice to catch this error explicitly
  32. archive.on('error', function(err) {
  33.   throw err;
  34. });

  35. // pipe archive data to the file
  36. archive.pipe(output);

  37. // append a file from stream
  38. const file1 = __dirname + '/file1.txt';
  39. archive.append(fs.createReadStream(file1), { name: 'file1.txt' });

  40. // append a file from string
  41. archive.append('string cheese!', { name: 'file2.txt' });

  42. // append a file from buffer
  43. const buffer3 = Buffer.from('buff it!');
  44. archive.append(buffer3, { name: 'file3.txt' });

  45. // append a file
  46. archive.file('file1.txt', { name: 'file4.txt' });

  47. // append files from a sub-directory and naming it `new-subdir` within the archive
  48. archive.directory('subdir/', 'new-subdir');

  49. // append files from a sub-directory, putting its contents at the root of archive
  50. archive.directory('subdir/', false);

  51. // append files from a glob pattern
  52. archive.glob('file*.txt', {cwd:__dirname});

  53. // finalize the archive (ie we are done appending files but streams have to finish yet)
  54. // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
  55. archive.finalize();
  56. ```

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.

_Formats will be changing in the future to implement a middleware approach._