archiver

a streaming interface for archive generation

MIT 84 个版本
安装
npm install archiver
yarn add archiver
pnpm add archiver
bun add 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.

版本列表
8.0.0 2026-05-08
7.0.1 2024-03-10
7.0.0 2024-02-28
6.0.2 2024-02-27
6.0.1 2023-09-04
6.0.0 2023-08-18
5.3.2 2023-08-17
5.3.1 2022-04-15
5.3.0 2021-03-07
5.2.0 2021-01-07
5.1.0 2020-11-20
5.0.2 2020-09-11
5.0.1 2020-09-11
5.0.0 2020-07-23
4.0.2 2020-07-11
4.0.1 2020-04-14
4.0.0 2020-04-14
3.1.1 2019-08-02
3.1.0 2019-08-02
3.0.3 2019-07-20
3.0.1 2019-07-19
3.0.0 2018-08-22
2.1.1 2018-01-10
2.1.0 2017-10-12
2.0.3 2017-08-26
2.0.2 2017-08-26
2.0.1 2017-08-26
2.0.0 2017-07-05
1.3.0 2016-12-13
1.2.0 2016-11-02
1.1.0 2016-08-29
1.0.1 2016-07-28
1.0.0 2016-04-06
0.21.0 2015-12-22
0.20.0 2015-11-30
0.19.0 2015-11-28
0.18.0 2015-11-28
0.17.0 2015-11-24
0.16.0 2015-10-17
0.15.1 2015-09-10
0.15.0-1 2015-06-05
0.15.0 2015-08-22
0.14.4 2015-05-20
0.14.3 2015-02-15
0.14.2 2015-01-23
0.14.1 2015-01-23
0.14.0 2015-01-23
0.13.1 2015-01-03
0.13.0 2014-11-28
0.12.0 2014-10-24
0.11.0-alpha 2014-07-13
0.11.0 2014-08-24
0.10.1 2014-06-17
0.10.0-alpha 2014-05-12
0.10.0 2014-05-29
0.9.1 2014-05-04
0.9.0 2014-04-19
0.8.1 2014-04-01
0.8.0 2014-04-01
0.7.1 2014-03-27
0.7.0 2014-03-23
0.6.1 2014-02-16
0.6.0 2014-02-15
0.5.2 2014-01-25
0.5.1 2014-01-15
0.5.0-alpha 2014-01-09
0.5.0 2014-01-11
0.4.10 2013-09-29
0.4.9 2013-09-03
0.4.8 2013-08-18
0.4.7 2013-08-18
0.4.6 2013-07-07
0.4.5 2013-06-25
0.4.4 2013-06-08
0.4.3 2013-04-18
0.4.2 2013-04-09
0.4.1 2013-03-13
0.4.0 2013-02-17
0.3.0 2013-01-27
0.2.2 2013-01-07
0.2.1 2013-01-04
0.2.0 2013-01-02
0.1.1 2012-11-15
0.1.0 2012-10-09