A lightweight streaming interface for creating ZIP and TAR archives in Node.js.
A modern, TypeScript-first rewrite of the popular archiver package.
archiver |
@archiver/archiver |
|
|---|---|---|
| Package Size | 43.1 kB | 38.8 kB |
| Install Size | 9.9 MB | 241.1 kB |
| Dependencies | 66 total | 3 direct, 4 total |
| Types | @types/archiver |
Built-in |
| ESM | CommonJS only | ESM only |
Tip
Coming from archiver? See the Migration Guide.
npm install @archiver/archiverpnpm add @archiver/archiveryarn add @archiver/archiverbun add @archiver/archiverNote
Requires Node.js 24 or later.
import { createWriteStream } from "node:fs";
import { ZipArchive } from "@archiver/archiver";
const output = createWriteStream("archive.zip");
const archive = new ZipArchive({ zlib: { level: 9 } });
archive.pipe(output);
// Append a file from disk
archive.file("package.json", { name: "package.json" });
// Append a string as a file
archive.append("Hello, world!", { name: "hello.txt" });
// Append an entire directory
archive.directory("src/", "src");
// Finalize — this resolves when the archive is complete
await archive.finalize();
console.log(`Archive created: ${archive.pointer()} bytes`);import { createWriteStream } from "node:fs";
import { TarArchive } from "@archiver/archiver";
const output = createWriteStream("archive.tar.gz");
const archive = new TarArchive({ gzip: true, gzipOptions: { level: 6 } });
archive.pipe(output);
archive.directory("dist/", "dist");
await archive.finalize();import { ZipArchive } from "@archiver/archiver";
const archive = new ZipArchive(options);| Option | Type | Default | Description |
|---|---|---|---|
comment |
string |
"" |
Archive comment |
forceUTC |
boolean |
false |
Use UTC timestamps |
forceLocalTime |
boolean |
false |
Force local timestamps |
forceZip64 |
boolean |
false |
Force ZIP64 headers |
namePrependSlash |
boolean |
false |
Prepend / to entry paths |
store |
boolean |
false |
Use STORE method (no compression) |
zlib |
ZlibOptions |
— | Compression options (e.g. { level: 9 }) |
statConcurrency |
number |
4 |
Parallel fs.stat workers |
highWaterMark |
number |
1048576 |
Stream buffer size (1 MB) |
import { TarArchive } from "@archiver/archiver";
const archive = new TarArchive(options);| Option | Type | Default | Description |
|---|---|---|---|
gzip |
boolean |
false |
Enable gzip compression (.tar.gz) |
gzipOptions |
ZlibOptions |
— | Gzip options (e.g. { level: 6 }) |
statConcurrency |
number |
4 |
Parallel fs.stat workers |
highWaterMark |
number |
1048576 |
Stream buffer size (1 MB) |
All methods return this for chaining, except where noted.
Add an entry from a Buffer, Stream, or string.
archive.append(Buffer.from("contents"), { name: "file.txt" });
archive.append("string contents", { name: "note.txt" });
archive.append(readableStream, { name: "data.bin" });data properties:
| Property | Type | Required | Description |
|---|---|---|---|
name |
string |
Yes | Entry name (including path) |
date |
Date |
No | Entry date |
mode |
number |
No | File permissions |
prefix |
string |
No | Path prefix for the entry name |
stats |
fs.Stats |
No | Pre-computed stats (avoids extra fs.stat calls) |
Add a file from disk. The file is stat'd and streamed automatically.
archive.file("/path/to/file.txt", { name: "renamed.txt" });Add a directory recursively. Pass a function as data to filter or transform entries.
// Add everything in src/ under the "source" directory
archive.directory("src/", "source");
// Filter entries
archive.directory("project/", "project", (entry) => {
if (entry.name.includes("node_modules")) return false;
return entry;
});Add files matching a glob pattern.
archive.glob("**/*.js", { cwd: "src/" }, { prefix: "scripts" });Create a symbolic link entry. Does not touch the filesystem.
archive.symlink("current", "releases/v1.0.0");Finalize the archive. Returns a Promise<void> that resolves when the archive is fully written. No more entries can be appended after calling this.
await archive.finalize();Abort the archiving process. Clears pending tasks, lets active workers finish, then ends the stream.
archive.abort();Returns the number of bytes emitted so far.
const bytes = archive.pointer();Both ZipArchive and TarArchive extend Node.js Transform streams and emit the following events:
| Event | Payload | Description |
|---|---|---|
entry |
EntryData |
Fired after an entry is processed |
progress |
{ entries: { total, processed }, fs: { totalBytes, processedBytes } } |
Progress update after each entry |
warning |
Error |
Non-fatal issue (e.g. stat failure) |
error |
Error |
Fatal error |
archive.on("progress", (progress) => {
console.log(
`${progress.entries.processed}/${progress.entries.total} entries`,
);
});
archive.on("warning", (err) => {
console.warn(err.message);
});See CONTRIBUTING.md for setup instructions and guidelines.
This project is a modern rewrite of the original archiver package by Chris Talkington.