JSZip is a JavaScript library for creating, reading, and editing .zip files, offering a straightforward API for both browser and Node.js environments. The current stable version is 3.10.1. It supports various data types for file content, including strings, ArrayBuffer, Uint8Array, Blob, and Promises, enabling flexible integration with modern web and server-side applications. While it has historically maintained a moderate release cadence, recent activity suggests a slower update cycle. Key differentiators include its robust asynchronous API for handling large files without blocking the UI, support for DEFLATE compression, and comprehensive TypeScript definitions. It's a foundational library for client-side archiving, often used in scenarios requiring dynamic zip generation or extraction within web applications.
npm install jszipVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating a zip file with text and binary content, including nested folders, and asynchronously generating a Blob to trigger a browser download.
Migrate all file content access and zip generation/loading calls to their asynchronous `*Async()` counterparts. Ensure all operations are `await`ed or chained with `.then()` for Promise-based execution. Review the upgrade guide for a full list of changes.
Always use the asynchronous `generateAsync()` and `loadAsync()` methods introduced in v3. Utilize Promise-based workflows to prevent UI blocking. For optimal performance with binary data, prefer using typed arrays (e.g., `Uint8Array`, `ArrayBuffer`) as input/output types rather than strings.
Upgrade JSZip to version 3.8.0 or newer. This version sanitizes filenames by removing relative path components (e.g., `../`). The original unsanitized filename is available as `unsafeOriginalName` if needed.
Avoid using JSZip with password-protected or multi-volume archives. If dealing with encrypted files, ensure they use AES encryption, which JSZip might support in specific contexts, but be aware that PKZIP 2.0 is not supported. For very large files, consider using Node.js stream-based APIs if applicable.
When fetching zip files via AJAX/XHR, ensure the response type is set to `arraybuffer` or `blob` to retrieve binary data correctly. For example: `xhr.responseType = 'arraybuffer';`.
Always use the asynchronous methods like `generateAsync()` and `loadAsync()`. These methods return Promises, allowing operations to run in the background without blocking the UI.
Enable `allowSyntheticDefaultImports: true` in your `tsconfig.json`. Alternatively, use `import * as JSZip from 'jszip';` or `import JSZip = require('jszip');` if you prefer explicit CommonJS import syntax in TypeScript.JSZip does not support PKZIP 2.0 encryption due to security concerns. If you need to handle encrypted zips, they must use AES encryption if JSZip offers any support for it, or use a different library. Avoid `zip -e` for JSZip-compatible archives.