adm-zip、archiver、jszip、yazl、zip-lib、zip-stream はいずれもJavaScriptでZIPファイルを扱うためのnpmパッケージですが、それぞれ設計思想や対応環境、機能範囲が大きく異なります。これらのライブラリは、ファイルの圧縮・展開、ストリーム処理、ブラウザ/Node.js対応といった観点で使い分けが必要です。例えば、フロントエンドでユーザーがアップロードしたファイルをZIP化する場合と、Node.jsサーバーで大容量ログを圧縮する場合では、最適な選択肢はまったく別になります。
WebアプリケーションでZIPファイルを扱う必要があるとき、どのnpmパッケージを選ぶべきか迷うことはよくあります。各ライブラリは異なる目的とアーキテクチャを持ち、特にフロントエンド開発者にとっては「ブラウザで動くか」「ストリーム対応か」「APIが使いやすいか」が重要な判断基準になります。ここでは、6つの主要なZIPライブラリを実用的な観点から徹底比較します。
まず最初に確認すべきは、ターゲット環境です。ZIP処理は重い操作になりがちなので、ブラウザで実行できるかどうかはアーキテクチャに大きく影響します。
jszip: ブラウザとNode.jsの両方で動作。File APIやBlobとの連携がスムーズ。zip-lib: ブラウザとNode.jsの両対応。PromiseベースのAPIが特徴。adm-zip: 純粋なNode.js向け。ファイルシステム操作に依存。archiver: ストリーム中心設計。Node.jsのfs.WriteStreamなどと組み合わせる。yazl: 圧縮のみの軽量ライブラリ。Node.js専用。zip-stream: archiverの内部で使われている低レベルライブラリ。Node.js専用。// jszip: ブラウザでBlobをZIP化
import JSZip from 'jszip';
const zip = new JSZip();
zip.file('hello.txt', 'Hello World');
const blob = await zip.generateAsync({ type: 'blob' });
// adm-zip: Node.jsでのファイル読み込み(ブラウザでは動作しない)
const AdmZip = require('adm-zip');
const zip = new AdmZip('./archive.zip');
const entry = zip.getEntry('file.txt');
console.log(zip.readAsText(entry));
💡 ポイント: フロントエンドでZIPを作成・展開する必要があるなら、
jszipかzip-libしか選択肢はありません。
ZIPライブラリは「圧縮だけ」「展開だけ」「両方対応」に分かれます。
| パッケージ | 圧縮 | 展開 |
|---|---|---|
adm-zip | ✅ | ✅ |
archiver | ✅ | ❌ |
jszip | ✅ | ✅ |
yazl | ✅ | ❌ |
zip-lib | ✅ | ✅ |
zip-stream | ✅ | ❌ |
archiver、yazl、zip-streamは圧縮専用です。既存ZIPファイルを読む必要があるなら、これらは使えません。
// yazl: 圧縮専用(展開はできない)
const yazl = require('yazl');
const zipfile = new yazl.ZipFile();
zipfile.addBuffer(Buffer.from('hello'), 'hello.txt');
zipfile.end();
zipfile.outputStream.pipe(fs.createWriteStream('out.zip'));
// jszip: 圧縮も展開も可能
const zip = new JSZip();
zip.file('test.txt', 'content');
const content = await zip.file('test.txt').async('string'); // 展開
adm-zip: ファイルパスを直接指定して操作できる。初心者向け。jszip: Promise/async対応で直感的。メモリ上での操作に最適。zip-lib: Promiseベースで一貫した非同期API。// adm-zip: ファイルパスで直接操作
const zip = new AdmZip('archive.zip');
zip.extractAllTo('./output/');
// zip-lib: PromiseでZIP作成
import { Zip } from 'zip-lib';
const zip = new Zip();
zip.addFile('text.txt', 'Hello');
await zip.archive('output.zip');
archiver: 大容量ファイル向け。メモリ使用量を抑えるストリーム処理。yazl / zip-stream: 最小限の機能で高速。カスタム統合向け。// archiver: ストリームで大容量ZIP作成
const archiver = require('archiver');
const archive = archiver('zip');
const output = fs.createWriteStream('large.zip');
archive.pipe(output);
archive.file('huge-file.bin', { name: 'data.bin' });
archive.finalize();
// zip-stream: archiverの内部で使われる低レベルAPI
const ZipStream = require('zip-stream');
const zip = new ZipStream();
zip.entry('data', { name: 'file.txt' }, () => {
zip.finish();
});
zip.pipe(fs.createWriteStream('out.zip'));
データの入力元(文字列、Buffer、ストリーム、ファイルパス)によっても選択が変わります。
jszip: 文字列、Uint8Array、Buffer、Blobを直接追加可能。archiver: ファイルパス、ストリーム、Bufferに対応。adm-zip: 主にファイルシステム上のファイルを操作。// jszip: 複数のデータソースを混在可能
zip.file('from-string.txt', 'Hello');
zip.file('from-buffer.bin', buffer);
zip.file('from-blob.dat', blob);
// archiver: ストリームを直接追加
archive.append(fs.createReadStream('input.txt'), { name: 'stream.txt' });
jszip// jszipで複数ファイルをZIP化
const zip = new JSZip();
files.forEach(file => zip.file(file.name, file));
const blob = await zip.generateAsync({ type: 'blob' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = 'files.zip'; a.click();
archiverconst archive = archiver('zip', { zlib: { level: 9 } });
archive.pipe(fs.createWriteStream('logs.zip'));
archive.file('app.log', { name: 'app.log' });
archive.finalize();
adm-zipconst zip = new AdmZip('input.zip');
zip.deleteFile('old.txt');
zip.addFile('new.txt', Buffer.from('updated'));
zip.writeZip('output.zip');
archiverapp.get('/download', (req, res) => {
res.attachment('files.zip');
const archive = archiver('zip');
archive.pipe(res);
archive.file('data.txt', { name: 'data.txt' });
archive.finalize();
});
adm-zip: メモリ上に全ZIP内容を展開するため、大容量ファイルには不向き。yazl / zip-stream: 圧縮専用かつ低レベルのため、初心者にはハードルが高い。jszip: ブラウザで大容量ファイルを処理するとUIがフリーズする可能性あり。archiver: 展開機能がないため、ZIPの中身を読む必要がある場合は別ライブラリと併用が必要。| 目的 | 推奨パッケージ |
|---|---|
| ブラウザでZIP作成/展開 | jszip |
| Node.jsでZIPを編集(追加/削除) | adm-zip |
| 大容量ファイルのストリーム圧縮 | archiver |
| 軽量で圧縮だけ必要なNode.js処理 | yazl |
| PromiseベースのシンプルAPI | zip-lib |
| カスタムZIPストリーム処理 | zip-stream |
jszipを検討してください。ブラウザ互換性と使いやすさが抜群です。archiver、小規模でファイル編集が必要ならadm-zipが無難です。yazl、ただし展開機能は別途実装が必要です。どのライブラリも一長一短があります。自分のプロジェクトの要件(環境、ファイルサイズ、必要な機能)に合わせて選んでください。
adm-zipはNode.js環境で既存ZIPファイルを読み込んで編集(ファイル追加・削除・更新)する必要がある場合に最適です。ファイルパスを直接指定して操作できるシンプルなAPIが特徴ですが、大容量ファイルにはメモリ使用量の面で不向きです。ブラウザでは動作しないため、フロントエンド用途には使えません。
archiverはNode.jsで大容量ファイルをストリーム処理でZIP圧縮する必要がある場合に選んでください。メモリ使用量を抑えながら効率的に圧縮でき、ExpressなどのHTTPレスポンスに直接パイプすることも可能です。ただし展開機能は持たず、純粋な圧縮専用ライブラリです。
jszipはブラウザとNode.jsの両方で動作し、ZIPの作成・展開を直感的なPromiseベースのAPIで行えるため、フロントエンド開発者に最もおすすめです。BlobやFile APIとの連携がスムーズで、ユーザーがアップロードした複数ファイルをZIP化してダウンロードさせるようなユースケースに最適です。
yazlは圧縮専用の軽量ライブラリで、Node.js環境で最小限のオーバーヘッドで高速にZIPを作成したい場合に適しています。ただし展開機能はなく、低レベルなストリームAPIしか提供しないため、初心者にはハードルが高いです。カスタム統合やパフォーマンスが最優先の場面で検討してください。
zip-libはPromiseベースのシンプルなAPIでZIPの作成・展開が可能なライブラリで、ブラウザとNode.jsの両方をサポートします。jszipより軽量でAPIが一貫しているのが特徴ですが、コミュニティやドキュメントの充実度はやや劣ります。シンプルなZIP操作だけで十分なプロジェクトに向いています。
zip-streamはarchiverの内部で使われている低レベルZIPストリームライブラリで、Node.js専用です。カスタムZIP生成ロジックを実装したい高度なユーザー向けですが、通常の用途ではarchiverを使う方が安全で簡単です。展開機能はなく、圧縮専用かつ生のストリーム操作が必要な特殊なケース以外では避けてください。
ADM-ZIP is a pure JavaScript implementation for zip data compression for NodeJS.
With npm do:
$ npm install adm-zip
Electron file system support described below.
The library allows you to:
There are no other nodeJS libraries that ADM-ZIP is dependent of
var AdmZip = require("adm-zip");
// reading archives
var zip = new AdmZip("./my_file.zip");
var password = "1234567890";
var zipEntries = zip.getEntries(); // an array of ZipEntry records - add password parameter if entries are password protected
zipEntries.forEach(function (zipEntry) {
console.log(zipEntry.toString()); // outputs zip entries information
if (zipEntry.entryName == "my_file.txt") {
console.log(zipEntry.getData().toString("utf8"));
}
});
// outputs the content of some_folder/my_file.txt
console.log(zip.readAsText("some_folder/my_file.txt"));
// extracts the specified file to the specified location
zip.extractEntryTo(/*entry name*/ "some_folder/my_file.txt", /*target path*/ "/home/me/tempfolder", /*maintainEntryPath*/ false, /*overwrite*/ true);
// extracts everything
zip.extractAllTo(/*target path*/ "/home/me/zipcontent/", /*overwrite*/ true);
// creating archives
var zip = new AdmZip();
// add file directly
var content = "inner content of the file";
zip.addFile("test.txt", Buffer.from(content, "utf8"), "entry comment goes here");
// add local file
zip.addLocalFile("/home/me/some_picture.png");
// get everything as a buffer
var willSendthis = zip.toBuffer();
// or write everything to disk
zip.writeZip(/*target file name*/ "/home/me/files.zip");
// ... more examples in the wiki
For more detailed information please check out the wiki.
ADM-ZIP has supported electron original-fs for years without any user interractions but it causes problem with bundlers like rollup etc. For continuing support original-fs or any other custom file system module. There is possible specify your module by fs option in ADM-ZIP constructor.
Example:
const AdmZip = require("adm-zip");
const OriginalFs = require("original-fs");
// reading archives
const zip = new AdmZip("./my_file.zip", { fs: OriginalFs });
.
.
.