archiver vs adm-zip vs jszip vs node-zip vs zip-a-folder vs zip-local
压缩和解压缩
archiveradm-zipjszipnode-zipzip-a-folderzip-local类似的npm包:

压缩和解压缩

压缩和解压缩是处理文件和文件夹的常见操作,尤其是在网络传输和存储时。压缩可以减少文件大小,节省带宽和存储空间,而解压缩则是将压缩文件恢复到原始状态。这些操作在许多应用程序中都很重要,例如文件管理器、云存储服务和数据备份工具。Node.js 提供了多种库来处理压缩和解压缩任务,其中包括 adm-ziparchiverjszipnode-zipzip-a-folderzip-local 等。这些库各有特点,适用于不同的场景和需求。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
archiver22,417,5812,94743.1 kB1552 年前MIT
adm-zip11,717,2992,159121 kB1482 年前MIT
jszip010,317762 kB409-(MIT OR GPL-3.0-or-later)
node-zip0217-2011 年前-
zip-a-folder07563 kB54 个月前MIT
zip-local012157.1 kB13--

功能对比: archiver vs adm-zip vs jszip vs node-zip vs zip-a-folder vs zip-local

压缩和解压缩

  • archiver:

    archiver 提供流式压缩,支持多种格式,适合大文件和按需压缩。

  • adm-zip:

    adm-zip 支持创建和解压缩 ZIP 文件,功能全面,易于使用。

  • jszip:

    jszip 支持创建、读取和修改 ZIP 文件,功能强大,适合跨平台应用。

  • node-zip:

    node-zip 支持简单的 ZIP 文件创建和解压,功能较为基础。

  • zip-a-folder:

    zip-a-folder 专注于快速压缩整个文件夹,功能简单直接。

  • zip-local:

    zip-local 支持文件和文件夹的压缩和解压,功能简单,适合本地使用。

流式处理

  • archiver:

    archiver 支持流式处理,适合大文件和实时压缩。

  • adm-zip:

    adm-zip 不支持流式处理,适合小文件和一次性操作。

  • jszip:

    jszip 不支持流式处理,适合离线处理文件。

  • node-zip:

    node-zip 不支持流式处理,适合简单的压缩任务。

  • zip-a-folder:

    zip-a-folder 不支持流式处理,适合快速压缩整个文件夹。

  • zip-local:

    zip-local 不支持流式处理,适合本地文件的压缩和解压。

跨平台支持

  • archiver:

    archiver 支持多种压缩格式,跨平台兼容性好。

  • adm-zip:

    adm-zip 仅支持 ZIP 格式,跨平台兼容性好。

  • jszip:

    jszip 支持在浏览器和 Node.js 中使用,跨平台兼容性最佳。

  • node-zip:

    node-zip 仅支持 ZIP 格式,适合 Node.js 环境。

  • zip-a-folder:

    zip-a-folder 仅支持 ZIP 格式,适合 Node.js 环境。

  • zip-local:

    zip-local 仅支持 ZIP 格式,适合本地文件处理。

示例代码

  • archiver:

    使用 archiver 创建流式 ZIP 文件

    const fs = require('fs');
    const archiver = require('archiver');
    
    const output = fs.createWriteStream('example.zip');
    const archive = archiver('zip');
    
    output.on('close', () => {
      console.log(`压缩完成,文件大小: ${archive.pointer()} 字节`);
    });
    
    archive.pipe(output);
    archive.append('Hello, World!', { name: 'hello.txt' });
    archive.finalize();
    
  • adm-zip:

    使用 adm-zip 创建和解压 ZIP 文件

    const AdmZip = require('adm-zip');
    
    // 创建 ZIP 文件
    const zip = new AdmZip();
    zip.addFile('hello.txt', Buffer.from('Hello, World!'));
    zip.writeZip('example.zip');
    
    // 解压 ZIP 文件
    const zip2 = new AdmZip('example.zip');
    zip2.extractAllTo('output', true);
    
  • jszip:

    使用 jszip 创建和读取 ZIP 文件

    const JSZip = require('jszip');
    const fs = require('fs');
    
    // 创建 ZIP 文件
    const zip = new JSZip();
    zip.file('hello.txt', 'Hello, World!');
    zip.generateAsync({ type: 'nodebuffer' }).then((content) => {
      fs.writeFileSync('example.zip', content);
    });
    
    // 读取 ZIP 文件
    fs.readFile('example.zip', (err, data) => {
      if (err) throw err;
      zip.loadAsync(data).then((zip) => {
        zip.file('hello.txt').async('string').then((content) => {
          console.log(content);
        });
      });
    });
    
  • node-zip:

    使用 node-zip 创建和解压 ZIP 文件

    const zip = require('node-zip')();
    const fs = require('fs');
    
    // 创建 ZIP 文件
    zip.file('hello.txt', 'Hello, World!');
    const data = zip.generate({ base64: false });
    fs.writeFileSync('example.zip', data, 'binary');
    
    // 解压 ZIP 文件
    const AdmZip = require('adm-zip');
    const zipFile = new AdmZip('example.zip');
    zipFile.extractAllTo('output', true);
    
  • zip-a-folder:

    使用 zip-a-folder 快速压缩文件夹

    const { zip } = require('zip-a-folder');
    
    zip('path/to/folder', 'output.zip').then(() => {
      console.log('文件夹压缩完成!');
    });
    
  • zip-local:

    使用 zip-local 压缩和解压文件

    const zipLocal = require('zip-local');
    
    // 压缩文件
    zipLocal.zip('path/to/folder').compress().then((zip) => {
      zip.write('output.zip');
      console.log('压缩完成!');
    });
    
    // 解压文件
    zipLocal.unzip('output.zip').then((folder) => {
      folder.write('output/folder');
      console.log('解压完成!');
    });
    

如何选择: archiver vs adm-zip vs jszip vs node-zip vs zip-a-folder vs zip-local

  • archiver:

    选择 archiver 如果您需要一个功能强大的库来创建压缩文件,支持多种格式(如 ZIP、TAR 等)。它提供了流式 API,适合处理大文件和需要按需压缩的场景。

  • adm-zip:

    选择 adm-zip 如果您需要一个简单易用的库来处理 ZIP 文件的读取和写入。它适合快速原型开发和小型项目,但可能不适合处理非常大的文件或需要高性能的应用。

  • jszip:

    选择 jszip 如果您需要在浏览器和 Node.js 中都能处理 ZIP 文件。它支持创建、读取和修改 ZIP 文件,适合需要跨平台兼容的应用。

  • node-zip:

    选择 node-zip 如果您需要一个轻量级的库来创建和解压 ZIP 文件。它的 API 简单,但功能相对有限,适合小型项目和简单的压缩任务。

  • zip-a-folder:

    选择 zip-a-folder 如果您需要快速压缩整个文件夹。它的使用非常简单,适合需要快速实现文件夹压缩功能的项目。

  • zip-local:

    选择 zip-local 如果您需要一个简单的库来处理本地文件和文件夹的压缩和解压缩。它支持多层文件夹压缩,适合小型项目和个人使用。

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.