archiver vs tar vs zip-a-folder
File Compression and Archiving
archivertarzip-a-folderSimilar Packages:

File Compression and Archiving

File compression and archiving libraries in Node.js provide tools to package multiple files and directories into a single file, reducing their size for storage or transmission. These libraries support various formats like ZIP, TAR, and GZ, allowing developers to create, extract, and manipulate archives programmatically. They are essential for tasks like backup, file transfer, and data packaging in web applications. archiver is a versatile library for creating ZIP and TAR archives with streaming support, while tar focuses on TAR file creation and extraction, and zip-a-folder specializes in creating ZIP files from entire directories with minimal configuration.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
archiver02,94543.1 kB1552 years agoMIT
tar09042.25 MB1013 days agoBlueOak-1.0.0
zip-a-folder07563 kB53 months agoMIT

Feature Comparison: archiver vs tar vs zip-a-folder

Supported Formats

  • archiver:

    archiver supports multiple formats, including ZIP, TAR, and GZ, making it a versatile choice for various archiving needs.

  • tar:

    tar focuses exclusively on TAR files, providing robust support for creating and extracting TAR archives, including handling compressed TAR.GZ and TAR.BZ2 files.

  • zip-a-folder:

    zip-a-folder specializes in ZIP file creation, particularly from entire directories, but does not support other archive formats.

Streaming Support

  • archiver:

    archiver provides excellent streaming support, allowing you to create archives on the fly without loading all data into memory, which is efficient for large files.

  • tar:

    tar also supports streaming for both creating and extracting TAR files, making it memory-efficient and suitable for handling large datasets.

  • zip-a-folder:

    zip-a-folder does not have built-in streaming support; it creates ZIP files by reading the entire folder structure into memory first.

Ease of Use

  • archiver:

    archiver has a more complex API due to its flexibility and support for multiple formats, but it is well-documented and easy to use once you understand its structure.

  • tar:

    tar offers a simple and straightforward API for working with TAR files, making it easy to integrate into projects without a steep learning curve.

  • zip-a-folder:

    zip-a-folder is very easy to use, with a minimalistic API that requires very little configuration, making it ideal for quick tasks.

Example Code

  • archiver:

    Creating a ZIP file with archiver

    const fs = require('fs');
    const archiver = require('archiver');
    
    const output = fs.createWriteStream('example.zip');
    const archive = archiver('zip');
    
    output.on('close', () => {
      console.log(`Archive created: ${archive.pointer()} total bytes`);
    });
    
    archive.on('error', (err) => {
      throw err;
    });
    
    archive.pipe(output);
    archive.append('Hello World!', { name: 'hello.txt' });
    archive.directory('folder/', 'folder');
    archive.finalize();
    
  • tar:

    Creating a TAR file with tar

    const fs = require('fs');
    const { tar } = require('tar');
    
    // Create a TAR file
    tar.create({
      file: 'example.tar',
      cwd: 'folder',
    }, ['file1.txt', 'file2.txt']);
    
    // Extract a TAR file
    tar.extract({
      file: 'example.tar',
      cwd: 'output',
    });
    
  • zip-a-folder:

    Zipping a folder with zip-a-folder

    const { zip } = require('zip-a-folder');
    
    zip('path/to/folder', 'output.zip')
      .then(() => console.log('Folder zipped successfully!'))
      .catch((err) => console.error('Error zipping folder:', err));
    

How to Choose: archiver vs tar vs zip-a-folder

  • archiver:

    Choose archiver if you need a flexible library that supports multiple archive formats (ZIP, TAR, etc.) and allows for streaming data, making it suitable for both small and large files.

  • tar:

    Choose tar if you specifically need to work with TAR files (creating, extracting, and manipulating) and prefer a library that is simple and efficient for handling TAR archives.

  • zip-a-folder:

    Choose zip-a-folder if you want a straightforward solution for zipping entire folders with minimal setup and configuration. It is ideal for quick and easy folder compression tasks.

README for archiver

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

// require modules
const fs = require('fs');
const archiver = require('archiver');

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

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

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

// 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;
});

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

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

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

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

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

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

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

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

// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
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.