archiver vs decompress-tar vs tar vs tar-fs vs tar-stream
Node.js 压缩与解压缩库
archiverdecompress-tartartar-fstar-stream类似的npm包:

Node.js 压缩与解压缩库

这些库用于处理文件的压缩和解压缩,尤其是在 Node.js 环境中。它们提供了不同的功能和接口,以便开发者能够方便地创建、读取和操作归档文件,如 tar 和 zip 格式。这些库在处理文件上传、下载以及存储时非常有用,能够提高文件传输的效率和管理的便利性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
archiver02,95539.6 kB1528 天前MIT
decompress-tar016-109 年前MIT
tar09142.26 MB98 天前BlueOak-1.0.0
tar-fs038118.2 kB12 个月前MIT
tar-stream043632.5 kB1416 天前MIT

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

压缩格式支持

  • archiver:

    Archiver 支持多种压缩格式,包括 zip 和 tar,允许用户根据需要选择合适的格式进行文件压缩。

  • decompress-tar:

    Decompress-tar 专注于 tar 格式的解压缩,提供简单的 API 来处理 tar 文件。

  • tar:

    Tar 主要用于创建和解压缩 tar 文件,支持基本的 tar 操作。

  • tar-fs:

    Tar-fs 允许将文件系统中的文件打包成 tar 文件,支持流式操作,适合大文件处理。

  • tar-stream:

    Tar-stream 是一个流式库,专注于高效地读取和写入 tar 文件,适合实时数据流处理。

流式支持

  • archiver:

    Archiver 支持流式压缩,可以在生成压缩文件的同时处理数据,适合大文件或实时数据的场景。

  • decompress-tar:

    Decompress-tar 提供流式解压缩功能,能够逐步读取 tar 文件,适合处理大文件。

  • tar:

    Tar 支持基本的流式操作,但不如其他库灵活。

  • tar-fs:

    Tar-fs 提供流式写入功能,允许将文件直接写入 tar 文件,适合实时数据处理。

  • tar-stream:

    Tar-stream 设计为流式库,能够高效地读取和写入 tar 文件,适合大数据量处理。

使用场景

  • archiver:

    适合需要创建压缩文件的场景,如文件上传和下载,数据备份等。

  • decompress-tar:

    适合需要解压缩 tar 文件的场景,如从 tar 文件中提取文件。

  • tar:

    适合需要基本 tar 文件操作的场景,如创建和解压缩 tar 文件。

  • tar-fs:

    适合需要将文件系统中的文件打包成 tar 文件的场景,尤其是大文件处理。

  • tar-stream:

    适合需要实时处理 tar 文件的场景,如流式数据处理。

性能

  • archiver:

    Archiver 在处理大文件时表现良好,能够高效地生成压缩文件,但可能在某些情况下占用较多内存。

  • decompress-tar:

    Decompress-tar 在解压缩 tar 文件时性能优越,能够快速处理大文件。

  • tar:

    Tar 是一个轻量级的库,性能良好,适合对性能有要求的应用。

  • tar-fs:

    Tar-fs 在处理流式写入时表现出色,适合需要高效处理的场景。

  • tar-stream:

    Tar-stream 在流式处理方面具有很高的性能,能够快速读取和写入 tar 文件。

学习曲线

  • archiver:

    Archiver 的 API 相对复杂,适合有一定经验的开发者使用。

  • decompress-tar:

    Decompress-tar 的 API 简单易用,适合初学者快速上手。

  • tar:

    Tar 的使用相对简单,适合有基本 Node.js 知识的开发者。

  • tar-fs:

    Tar-fs 的 API 直观,适合需要流式操作的开发者。

  • tar-stream:

    Tar-stream 的学习曲线较陡,但对于需要高效流式处理的开发者来说是值得的。

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

  • archiver:

    选择 Archiver 如果你需要一个功能丰富的库来创建多种格式的压缩文件(如 zip 和 tar),并且需要支持流式操作和文件夹的压缩。它适合于需要生成压缩文件的场景。

  • decompress-tar:

    选择 Decompress-tar 如果你的主要需求是解压缩 tar 文件,并且希望使用简单的 API 来处理文件流。它专注于 tar 格式,适合需要快速解压缩的场景。

  • tar:

    选择 Tar 如果你需要一个轻量级的库来处理 tar 文件的创建和解压缩。它提供了基本的 tar 文件操作,适合对性能有较高要求的应用。

  • tar-fs:

    选择 Tar-fs 如果你需要将文件系统中的文件打包成 tar 文件,并且希望支持流式操作。它适合需要将文件流直接写入 tar 文件的场景。

  • tar-stream:

    选择 Tar-stream 如果你需要一个高效的流式 tar 处理库,能够在流中读取和写入 tar 文件。它适合需要处理大文件或实时数据流的应用。

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

import fs from "fs";
import { ZipArchive } from "archiver";

// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + "/example.zip");
const archive = new ZipArchive({
  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.