archiver vs tar vs tar-fs vs tar-stream vs zip-stream
Node.js 压缩与归档库
archivertartar-fstar-streamzip-stream类似的npm包:

Node.js 压缩与归档库

这些库用于在 Node.js 环境中处理文件的压缩和归档。它们提供了不同的功能和接口,使开发者能够轻松地创建、读取和操作归档文件(如 ZIP 和 TAR 格式)。选择合适的库可以帮助提高文件处理的效率和灵活性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
archiver02,95343.1 kB1562 年前MIT
tar09092.24 MB102 天前BlueOak-1.0.0
tar-fs038218.2 kB121 天前MIT
tar-stream043732.1 kB1825 天前MIT
zip-stream01669.33 kB271 年前MIT

功能对比: archiver vs tar vs tar-fs vs tar-stream vs zip-stream

支持的格式

  • archiver:

    Archiver 支持多种压缩格式,包括 ZIP 和 TAR,适合需要多种格式支持的应用。

  • tar:

    Tar 主要支持 TAR 格式,适合只需处理 TAR 文件的简单应用。

  • tar-fs:

    Tar-fs 专注于 TAR 格式,适合需要将文件系统直接归档为 TAR 的场景。

  • tar-stream:

    Tar-stream 也支持 TAR 格式,提供灵活的流式处理能力,适合需要逐步处理的应用。

  • zip-stream:

    Zip-stream 专注于 ZIP 格式,适合需要动态生成 ZIP 文件的应用。

流式处理

  • archiver:

    Archiver 提供流式 API,允许逐步写入文件,适合处理大文件或多个文件的场景。

  • tar:

    Tar 提供基本的流式支持,但不如 Archiver 灵活,适合简单的归档需求。

  • tar-fs:

    Tar-fs 允许将文件系统的文件直接流式归档,适合处理大文件或目录。

  • tar-stream:

    Tar-stream 提供强大的流式 API,允许逐步读取和写入 TAR 文件,适合需要高控制能力的应用。

  • zip-stream:

    Zip-stream 支持流式写入,适合动态生成 ZIP 文件,能够逐步添加文件。

易用性

  • archiver:

    Archiver 提供友好的 API 和丰富的文档,易于上手,适合各种开发者。

  • tar:

    Tar 的 API 简单明了,适合快速实现基本功能,但功能较为有限。

  • tar-fs:

    Tar-fs 的 API 直观,适合需要快速归档文件的开发者。

  • tar-stream:

    Tar-stream 的 API 灵活,但需要一定的学习成本,适合需要高级功能的开发者。

  • zip-stream:

    Zip-stream 的 API 设计简单,易于使用,适合需要快速生成 ZIP 文件的场景。

性能

  • archiver:

    Archiver 在处理大文件和多个文件时表现良好,支持多种压缩算法,能够优化性能。

  • tar:

    Tar 在处理 TAR 文件时性能优秀,但不支持压缩,适合简单归档。

  • tar-fs:

    Tar-fs 在处理文件系统归档时性能高效,适合大文件处理。

  • tar-stream:

    Tar-stream 提供高效的流式处理,适合需要逐步处理的应用,性能表现良好。

  • zip-stream:

    Zip-stream 在生成 ZIP 文件时性能良好,适合动态添加文件的场景。

社区与维护

  • archiver:

    Archiver 拥有活跃的社区和定期更新,提供良好的支持和文档。

  • tar:

    Tar 是一个成熟的库,维护稳定,但更新频率较低。

  • tar-fs:

    Tar-fs 社区较小,但功能稳定,适合特定需求。

  • tar-stream:

    Tar-stream 由社区维护,更新频率适中,适合需要灵活处理的开发者。

  • zip-stream:

    Zip-stream 拥有活跃的社区,提供良好的文档和支持,适合快速开发。

如何选择: archiver vs tar vs tar-fs vs tar-stream vs zip-stream

  • archiver:

    选择 Archiver 如果你需要一个功能丰富且易于使用的库,支持多种格式(如 ZIP 和 TAR),并且需要流式处理和压缩选项。它适合需要创建复杂归档的应用。

  • tar:

    选择 Tar 如果你只需要处理 TAR 格式的文件,且希望使用一个简单且高效的库。它提供了基本的归档功能,适合简单的归档需求。

  • tar-fs:

    选择 Tar-fs 如果你需要将文件系统中的文件直接流式归档为 TAR 格式,并且希望与文件流结合使用。它适合需要处理大文件或目录的场景。

  • tar-stream:

    选择 Tar-stream 如果你需要一个灵活的流式 API 来处理 TAR 文件的读取和写入。它适合需要逐步处理文件的应用,提供了更高的控制能力。

  • zip-stream:

    选择 Zip-stream 如果你需要一个流式的 ZIP 文件创建工具,适合需要动态生成 ZIP 文件的场景。它支持逐步添加文件,适合大文件的处理。

archiver的README

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.