zip-lib vs adm-zip vs node-zip vs yazl vs zip-a-folder
Node.jsのZIPライブラリ
zip-libadm-zipnode-zipyazlzip-a-folder類似パッケージ:

Node.jsのZIPライブラリ

Node.jsのZIPライブラリは、ファイルやフォルダをZIP形式で圧縮したり、解凍したりするためのツールです。これらのライブラリは、データの圧縮、アーカイブの作成、ファイルの転送を効率化し、ストレージの使用を最適化するのに役立ちます。特に、ウェブアプリケーションやサーバーサイドのスクリプトで、データを圧縮して送信する際に非常に便利です。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
zip-lib152,7484270.2 kB17日前MIT
adm-zip02,161122 kB14419日前MIT
node-zip0217-2011年前-
yazl037558.7 kB191年前MIT
zip-a-folder076212 kB05日前MIT

機能比較: zip-lib vs adm-zip vs node-zip vs yazl vs zip-a-folder

APIのシンプルさ

  • zip-lib:

    zip-libは、柔軟なAPIを提供し、カスタマイズが可能です。特に、特定のニーズに合わせた機能を追加したい場合に適しています。

  • adm-zip:

    adm-zipはシンプルで直感的なAPIを提供しており、ZIPファイルの作成や解凍が簡単に行えます。特に、少ないコードで操作できるため、初心者にも優しいです。

  • node-zip:

    node-zipはストリームベースのAPIを提供しており、少し複雑ですが、より柔軟な操作が可能です。大きなファイルを扱う際には、ストリーム処理が役立ちます。

  • yazl:

    yazlは、シンプルなAPIを持ちながらも、高度な機能を提供します。特に、圧縮のパフォーマンスを重視する開発者に向いています。

  • zip-a-folder:

    zip-a-folderは、コマンドラインインターフェース(CLI)を通じて簡単にフォルダをZIP圧縮できるシンプルなAPIを提供します。

パフォーマンス

  • zip-lib:

    zip-libは、カスタム圧縮オプションを提供し、特定のニーズに応じたパフォーマンスを引き出すことができます。

  • adm-zip:

    adm-zipは、一般的な用途には十分なパフォーマンスを提供しますが、大きなファイルの処理には向いていない場合があります。

  • node-zip:

    node-zipは、ストリーム処理を利用することで、メモリ使用量を抑えつつ大きなファイルを効率的に処理できます。

  • yazl:

    yazlは、特に大きなファイルの圧縮において高いパフォーマンスを発揮します。圧縮率も高く、効率的な処理が可能です。

  • zip-a-folder:

    zip-a-folderは、フォルダ全体を圧縮する際に便利で、迅速に処理を行いますが、個別のファイル操作には向いていません。

使用シナリオ

  • zip-lib:

    高度な機能やカスタマイズが必要な場合、特に大規模なプロジェクトに適しています。

  • adm-zip:

    小規模なプロジェクトや、簡単なZIPファイルの作成・解凍が必要なシナリオに最適です。

  • node-zip:

    大きなファイルをストリームで処理する必要がある場合に適しています。特に、メモリを節約したい場合に有効です。

  • yazl:

    高パフォーマンスが求められるシナリオ、特に大きなデータセットを扱う際に最適です。

  • zip-a-folder:

    フォルダ全体を簡単に圧縮したい場合、特にCLIを使用するシナリオに便利です。

拡張性

  • zip-lib:

    zip-libは、柔軟な設計により、さまざまな機能を追加できるため、高い拡張性を持っています。

  • adm-zip:

    adm-zipは基本的な機能に特化しており、拡張性は限られていますが、シンプルさが魅力です。

  • node-zip:

    node-zipはストリームベースのアプローチを採用しており、拡張性が高く、複雑な処理にも対応できます。

  • yazl:

    yazlは、圧縮アルゴリズムの選択肢があり、拡張性が高いです。特に、特定のニーズに応じたカスタマイズが可能です。

  • zip-a-folder:

    zip-a-folderは、主にフォルダ圧縮に特化しており、拡張性はあまりありません。

メンテナンス

  • zip-lib:

    zip-libは、柔軟性が高い反面、メンテナンスには注意が必要ですが、カスタマイズ性が高いです。

  • adm-zip:

    adm-zipはシンプルな設計のため、メンテナンスが容易です。

  • node-zip:

    node-zipはストリーム処理に特化しているため、メンテナンスはやや複雑ですが、効率的です。

  • yazl:

    yazlは、パフォーマンスを重視した設計で、メンテナンスは比較的簡単です。

  • zip-a-folder:

    zip-a-folderは、シンプルなAPIを持ち、メンテナンスが容易です。

選び方: zip-lib vs adm-zip vs node-zip vs yazl vs zip-a-folder

  • zip-lib:

    柔軟性と拡張性を重視する場合に選択してください。カスタム圧縮オプションや高度な機能を必要とするプロジェクトに適しています。

  • adm-zip:

    簡単なZIPファイルの作成や解凍が必要な場合に最適です。シンプルなAPIを提供し、特に小規模なプロジェクトに適しています。

  • node-zip:

    ストリームベースの操作が必要な場合に選択すべきです。大きなファイルの処理や、メモリ使用量を抑えたい場合に有効です。

  • yazl:

    高性能なZIPファイルの作成が必要な場合に適しています。特に大きなファイルを扱う際に、効率的な圧縮を提供します。

  • zip-a-folder:

    フォルダ全体を簡単にZIP圧縮したい場合に便利です。CLIからも使用でき、使い勝手が良いです。

zip-lib のREADME

zip-lib

A zip and unzip library for Node.js with promise-based APIs, support for compressing to Buffer or extracting from Buffer, and advanced features like entry filtering, cancellation, and symlink-aware handling.

npm Package GitHub license node Build Status

Install

npm install zip-lib

Quick Start

Zip

You can use zip-lib to compress files or folders.

Zip single file

import * as zl from "zip-lib";

async function zipToFile() {
	try {
		await zl.archiveFile("path/to/file.txt", "path/to/target.zip");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

async function zipToBuffer() {
	try {
		const buffer = await zl.archiveFile("path/to/file.txt");
		console.log("done", buffer.byteLength);
	} catch (error) {
		console.error(error);
	}
}

Zip single folder

import * as zl from "zip-lib";

async function zipToFile() {
	try {
		await zl.archiveFolder("path/to/folder", "path/to/target.zip");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

async function zipToBuffer() {
	try {
		const buffer = await zl.archiveFolder("path/to/folder");
		console.log("done", buffer.byteLength);
	} catch (error) {
		console.error(error);
	}
}

Unzip

import * as zl from "zip-lib";

async function unzipFromFile() {
	try {
		await zl.extract("path/to/target.zip", "path/to/target");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

async function unzipFromBuffer(zipBuffer: Buffer) {
	try {
		await zl.extract(zipBuffer, "path/to/target");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

Advanced usage

Set the compression level

import * as zl from "zip-lib";

async function zipToFile() {
	try {
		await zl.archiveFolder("path/to/folder", "path/to/target.zip", {
			compressionLevel: 9,
		});
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

Zip multiple files and folders

import * as zl from "zip-lib";

async function zipToFile() {
	try {
		const zip = new zl.Zip();
		// Adds a file from the file system
		zip.addFile("path/to/file.txt");
		// Adds a folder from the file system, putting its contents at the root of the archive
		zip.addFolder("path/to/folder");
		// Generate the zip file.
		await zip.archive("path/to/target.zip");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

The path/to/folder directory is as follows:

path/to/folder
.
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

And the contents of the generated path/to/target.zip archive will be as follows:

path/to/target.zip
.
├── file.txt
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

Zip with metadata

import * as zl from "zip-lib";

async function zipToFile() {
	try {
		const zip = new zl.Zip();
		// Adds a file from the file system
		zip.addFile("path/to/file.txt", "renamedFile.txt");
		zip.addFile("path/to/file2.txt", "folder/file.txt");
		// Adds a folder from the file system and names it `new folder` within the archive
		zip.addFolder("path/to/folder", "new folder");
		// Generate the zip file.
		zip.archive("path/to/target.zip");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

The path/to/folder directory is as follows:

path/to/folder
.
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

And the contents of the generated path/to/target.zip archive will be as follows:

path/to/target.zip
.
├── renamedFile.txt
├── folder
│   ├── file.txt
├── new folder
    ├── dir1
    │   ├── file.ext
    ├── dir2
    └── file_in_root.ext

Unzip with entry callback

Using the onEntry callback, we can track extraction progress and control the extraction process. See IExtractOptions.

import * as zl from "zip-lib";

async function unzipFromFile() {
	try {
		const unzip = new zl.Unzip({
			// Called before an item is extracted.
			onEntry: (event) => {
				console.log(event.entryCount, event.entryName);
			},
		});
		await unzip.extract("path/to/target.zip", "path/to/target");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

Unzip and exclude specified entries

The following code shows how to exclude the __MACOSX folder in the zip file when extracting. See IExtractOptions.

import * as zl from "zip-lib";

async function unzipFromFile() {
	try {
		const unzip = new zl.Unzip({
			// Called before an item is extracted.
			onEntry: (event) => {
				if (/^__MACOSX\//.test(event.entryName)) {
					// entry name starts with __MACOSX/
					event.preventDefault();
				}
			},
		});
		await unzip.extract("path/to/target.zip", "path/to/target");
		console.log("done");
	} catch (error) {
		console.error(error);
	}
}

Cancel zip

If the cancel method is called after the archive is complete, nothing will happen.

import * as zl from "zip-lib";

const zip = new zl.Zip();
async function zipToFile() {
	try {
		zip.addFile("path/to/file.txt");
		await zip.archive("path/to/target.zip");
		console.log("done");
	} catch (error) {
		if (error instanceof Error && error.name === "Canceled") {
			console.log("cancel");
		} else {
			console.log(error);
		}
	}
}

function cancel() {
	zip.cancel();
}

Cancel unzip

If the cancel method is called after the extract is complete, nothing will happen.

import * as zl from "zip-lib";

const unzip = new zl.Unzip();
async function unzipFromFile() {
	try {
		await unzip.extract("path/to/target.zip", "path/to/target");
		console.log("done");
	} catch (error) {
		if (error instanceof Error && error.name === "Canceled") {
			console.log("cancel");
		} else {
			console.log(error);
		}
	}
}

function cancel() {
	unzip.cancel();
}

API

Choose the API based on how much control you need:

  • Use archiveFile, archiveFolder, and extract for simple one-shot operations.
  • Use Zip when you need to add multiple files or folders, rename entries, or cancel compression.
  • Use Unzip when you need to filter entries with onEntry or cancel extraction.

Method: archiveFile

Compress a single file.

archiveFile(file: string, options?: IZipOptions): Promise<Buffer>
archiveFile(file: string, zipFile: string, options?: IZipOptions): Promise<void>
  • file: Path to the source file.
  • zipFile: Optional output zip file path.
  • options: Optional IZipOptions.

If zipFile is provided, the archive is written to disk and the method returns Promise<void>. Otherwise it returns the generated zip as a Buffer.

Method: archiveFolder

Compress all contents of a folder.

archiveFolder(folder: string, options?: IZipOptions): Promise<Buffer>
archiveFolder(folder: string, zipFile: string, options?: IZipOptions): Promise<void>
  • folder: Path to the source folder.
  • zipFile: Optional output zip file path.
  • options: Optional IZipOptions.

If zipFile is provided, the archive is written to disk and the method returns Promise<void>. Otherwise it returns the generated zip as a Buffer.

Method: extract

Extract a zip file or zip buffer to a target folder.

extract(zipFile: string, targetFolder: string, options?: IExtractOptions): Promise<void>
extract(zipBuffer: Buffer, targetFolder: string, options?: IExtractOptions): Promise<void>
  • zipFile: Path to a zip file on disk.
  • zipBuffer: A zip archive in memory.
  • targetFolder: Destination folder.
  • options: Optional IExtractOptions.

Class: Zip

Build an archive incrementally when the top-level helpers are not enough.

Constructor

new Zip(options?: IZipOptions)

Methods

addFile(file: string, metadataPath?: string): void
addFolder(folder: string, metadataPath?: string): void
archive(): Promise<Buffer>
archive(zipFile: string): Promise<void>
cancel(): void
  • addFile: Adds one file to the archive.
  • addFolder: Adds one folder to the archive.
  • metadataPath: Optional path stored inside the zip. A valid metadataPath must not start with / or /[A-Za-z]:\//, and must not contain ...
  • archive(): Returns the zip as a Buffer.
  • archive(zipFile): Writes the zip directly to disk.
  • cancel(): Cancels compression. Calling it after completion has no effect.

Use metadataPath to rename files or folders inside the archive. It is typically computed with path.relative(root, realPath).

Class: Unzip

Extract with entry filtering and cancellation support.

Constructor

new Unzip(options?: IExtractOptions)

Methods

extract(zipFile: string, targetFolder: string): Promise<void>
extract(zipBuffer: Buffer, targetFolder: string): Promise<void>
cancel(): void
  • extract(...): Extracts from a zip file path or a Buffer.
  • cancel(): Cancels extraction. Calling it after completion has no effect.

Options: IZipOptions

interface IZipOptions {
  followSymlinks?: boolean;
  compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
}
  • followSymlinks: Default false. If true, adds the target of a symbolic link. If false, adds the symbolic link itself.
  • compressionLevel: Default 6. Use 0 to store file data without compression, or 1-9 to deflate file data.

Options: IExtractOptions

interface IExtractOptions {
  overwrite?: boolean;
  safeSymlinksOnly?: boolean;
  symlinkAsFileOnWindows?: boolean;
  onEntry?: (event: IEntryEvent) => void;
}
  • overwrite: Default false. If true, deletes the target directory before extraction.
  • safeSymlinksOnly: Default false. If true, refuses to create symlinks whose targets are outside the extraction root.
  • symlinkAsFileOnWindows: Default true. On Windows, if true, symbolic links are extracted as regular files. If false, the library tries to create real symbolic links instead. This may fail with EPERM when the current process is not allowed to create symlinks. Keep the default for better compatibility; set this to false only if you want to preserve symlinks as symlinks.
  • onEntry: Called before an entry is extracted. Use it to inspect or skip entries.

WARNING: On Windows, creating symbolic links may require administrator privileges, depending on system policy. If Windows Developer Mode is enabled, non-administrator processes can usually create symlinks as well. If symlinkAsFileOnWindows is false and the current process is not allowed to create symlinks, extraction may fail with EPERM.

Event: IEntryEvent

interface IEntryEvent {
  readonly entryName: string;
  readonly entryCount: number;
  preventDefault(): void;
}
  • entryName: The current entry name.
  • entryCount: The total number of entries in the archive.
  • preventDefault(): Skips the current entry.

License

Licensed under the MIT license.