JSZip

3.10.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import JSZip from 'jszip';

async function createAndDownloadZip() {
  const zip = new JSZip();

  zip.file("Hello.txt", "Hello World\n");

  const imgFolder = zip.folder("images");
  // Assume imgData is a base64 encoded string of an image
  const imgData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='; // 1x1 transparent PNG
  imgFolder.file("smile.gif", imgData, { base64: true });

  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 });

  try {
    const content = await zip.generateAsync({ type: "blob", compression: "DEFLATE" });
    // In a browser, you would typically use a library like FileSaver.js
    // or create a URL object to trigger a download.
    const url = URL.createObjectURL(content);
    const a = document.createElement('a');
    a.href = url;
    a.download = "example.zip";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
    console.log("Zip file generated and download initiated.");
  } catch (error) {
    console.error("Error generating zip file:", error);
  }
}

// Call the function to create and download the zip
createAndDownloadZip();

view raw JSON →