{"id":11195,"library":"jszip","title":"JSZip","description":"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.","status":"active","version":"3.10.1","language":"javascript","source_language":"en","source_url":"https://github.com/Stuk/jszip","tags":["javascript","zip","deflate","inflate","typescript"],"install":[{"cmd":"npm install jszip","lang":"bash","label":"npm"},{"cmd":"yarn add jszip","lang":"bash","label":"yarn"},{"cmd":"pnpm add jszip","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for DEFLATE compression and decompression functionality.","package":"pako","optional":false}],"imports":[{"note":"CommonJS `require` is typical in Node.js, but `import JSZip from 'jszip'` is the idiomatic ESM import, often requiring `allowSyntheticDefaultImports` in TypeScript or bundler configuration to treat the CommonJS export as a default. For strict CommonJS in TypeScript, use `import JSZip = require('jszip');`.","wrong":"const JSZip = require('jszip');","symbol":"JSZip","correct":"import JSZip from 'jszip';"},{"note":"loadAsync is a static method on the JSZip class, not a named export. Ensure you import the class itself to access this method. Available since v3.0.0.","wrong":"import { loadAsync } from 'jszip';","symbol":"JSZip.loadAsync","correct":"import JSZip from 'jszip';\nconst zip = await JSZip.loadAsync(data);"},{"note":"support is a static property on the JSZip class, used for feature detection (e.g., support for ArrayBuffer, Blob, Promises, Node.js Buffer).","wrong":"import { support } from 'jszip';","symbol":"JSZip.support","correct":"import JSZip from 'jszip';\nif (JSZip.support.blob) { /* ... */ }"}],"quickstart":{"code":"import JSZip from 'jszip';\n\nasync function createAndDownloadZip() {\n  const zip = new JSZip();\n\n  zip.file(\"Hello.txt\", \"Hello World\\n\");\n\n  const imgFolder = zip.folder(\"images\");\n  // Assume imgData is a base64 encoded string of an image\n  const imgData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='; // 1x1 transparent PNG\n  imgFolder.file(\"smile.gif\", imgData, { base64: true });\n\n  zip.file(\"nested/path/document.pdf\", new Uint8Array([73, 84, 73, 83, 32, 65, 32, 80, 68, 70, 32, 70, 73, 76, 69]), { binary: true });\n\n  try {\n    const content = await zip.generateAsync({ type: \"blob\", compression: \"DEFLATE\" });\n    // In a browser, you would typically use a library like FileSaver.js\n    // or create a URL object to trigger a download.\n    const url = URL.createObjectURL(content);\n    const a = document.createElement('a');\n    a.href = url;\n    a.download = \"example.zip\";\n    document.body.appendChild(a);\n    a.click();\n    document.body.removeChild(a);\n    URL.revokeObjectURL(url);\n    console.log(\"Zip file generated and download initiated.\");\n  } catch (error) {\n    console.error(\"Error generating zip file:\", error);\n  }\n}\n\n// Call the function to create and download the zip\ncreateAndDownloadZip();","lang":"typescript","description":"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."},"warnings":[{"fix":"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.","message":"JSZip v3 introduced significant breaking changes, shifting from synchronous to exclusively asynchronous APIs for file operations and generation. Methods like `generate()` were replaced by `generateAsync()`, synchronous content getters (e.g., `asText()`) by `async()`, and `load()` by `loadAsync()`. The `type` option in `generateAsync()` became mandatory, and `createFolders` option now defaults to `true`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"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.","message":"Handling large zip files or numerous small files synchronously can lead to browser unresponsiveness or crashes, particularly in older browsers like IE <= 9. JSZip's synchronous APIs are less performant for substantial workloads.","severity":"gotcha","affected_versions":"<3.0.0"},{"fix":"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.","message":"Versions prior to 3.8.0 were vulnerable to 'zip slip' path traversal attacks when using `loadAsync()` with untrusted zip files, which could allow malicious files to be written outside the intended directory.","severity":"breaking","affected_versions":"<3.8.0"},{"fix":"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.","message":"JSZip does not support all features of the ZIP specification. Specifically, encrypted zip files (especially those using older encryption methods like PKZIP 2.0, common from macOS `zip -e`), multi-volume archives, or extremely large ZIP64 files (where 64-bit integers exceed JavaScript's safe integer limits) are not fully supported, leading to failed `loadAsync()` operations.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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';`.","cause":"This error often occurs when binary zip content is incorrectly handled, such as an AJAX request decoding binary data as a text string, leading to data corruption.","error":"Corrupted zip or bug: unexpected signature"},{"fix":"Always use the asynchronous methods like `generateAsync()` and `loadAsync()`. These methods return Promises, allowing operations to run in the background without blocking the UI.","cause":"Attempting to process large amounts of data using JSZip's older synchronous APIs (pre-v3) can block the main thread, causing the browser to freeze.","error":"My browser crashes / becomes unresponsive / never finish the execution."},{"fix":"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.","cause":"When using TypeScript with `import JSZip from 'jszip'` for a library that primarily uses CommonJS `module.exports = JSZip;`, this error indicates that TypeScript's module resolution is strict about default exports.","error":"Module 'jszip' has no default export."},{"fix":"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.","cause":"Trying to decompress a password-protected zip file that uses an unsupported encryption method, such as PKZIP 2.0 (often generated by `zip -e` on macOS), which JSZip does not support.","error":"Encrypted zip: unsupported encrypt method"}],"ecosystem":"npm"}