Create, read and edit .zip files with JavaScript http://stuartk.com/jszip
JSZip is a mature JavaScript library that enables ZIP archive manipulation directly in JavaScript environments without native dependencies. With over 20 million weekly downloads, it powers applications ranging from document processing tools to file bundlers, providing a consistent API across both browser and Node.js platforms.
The library fills a critical gap in JavaScript's standard library by implementing the ZIP format specification entirely in JavaScript. This approach eliminates the need for platform-specific binaries or command-line tools, making it particularly valuable for web applications that need to generate downloads, process uploaded archives, or handle complex file formats like DOCX and XLSX which are ZIP-based.
JSZip supports both in-memory and streaming operations, allowing developers to handle archives of varying sizes efficiently. It includes compression (DEFLATE), decompression, folder hierarchies, file metadata, and can output to multiple formats including Blob, ArrayBuffer, and Node.js Buffer. The library is dual-licensed under MIT or GPL-3.0-or-later, providing flexibility for both open-source and commercial projects.
Typical users include developers building document processors, data export tools, browser-based IDEs, offline-capable web apps, and any application requiring client-side or server-side ZIP manipulation without external dependencies.
import JSZip from 'jszip';
import { writeFile } from 'fs/promises';
// Create a new ZIP archive
const zip = new JSZip();
// Add text file
zip.file('README.txt', 'Project documentation\nVersion 1.0');
// Add nested folder structure
const imgFolder = zip.folder('assets/images');
imgFolder.file('logo.svg', '<svg>...</svg>');
// Add binary data (simulated)
const jsonData = JSON.stringify({ users: [{ id: 1, name: 'Alice' }] });
zip.file('data/export.json', jsonData);
// Generate ZIP with compression
const content = await zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: { level: 9 }
});
await writeFile('output.zip', content);
// Read and extract ZIP
const existingZip = await JSZip.loadAsync(content);
const readmeContent = await existingZip.file('README.txt').async('string');
console.log(readmeContent);
// List all files using forEach
existingZip.forEach((relativePath, file) => {
console.log(`Found: ${relativePath}, dir: ${file.dir}`);
});
// Filter files with RegExp
const jsonFiles = existingZip.file(/\.json$/);
for (const file of jsonFiles) {
const data = await file.async('string');
console.log(`JSON content: ${data}`);
}Client-side file bundling: Generate ZIP archives in the browser to let users download multiple files as a single package, such as exporting a collection of images, reports, or project files without server round-trips.
Office document processing: Parse and manipulate DOCX, XLSX, PPTX, and ODF files, which are ZIP archives containing XML. Extract specific content, modify metadata, or generate documents programmatically in pure JavaScript.
Backup and export features: Create compressed backups of user data in web applications, including JSON configurations, SQLite databases, or local storage contents, then trigger browser downloads or upload to cloud storage.
Archive extraction and validation: Process user-uploaded ZIP files to extract contents, validate structure, scan for specific files, or convert formats entirely client-side without exposing file contents to servers.
Build tooling and asset bundling: Integrate into Node.js build pipelines to package assets, create deployment archives, or process dependencies that arrive as ZIP files, leveraging the streaming API for memory efficiency.
npm install jszippnpm add jszipbun add jszip