Create, read and edit .zip files with JavaScript in both browser and Node.js environments
npx @tessl/cli install tessl/npm-jszip@3.10.0JSZip is a comprehensive JavaScript library for creating, reading, and editing ZIP archives in both browser and Node.js environments. It provides a simple and intuitive API for ZIP file manipulation, supporting various data formats including strings, arrays, Uint8Arrays, ArrayBuffers, and Blobs. The library features async/sync operations for generating ZIP files, supports compression with DEFLATE algorithm, handles folder structures, and includes comprehensive TypeScript definitions.
npm install jszipimport JSZip from "jszip";For CommonJS:
const JSZip = require("jszip");import JSZip from "jszip";
// Create a new ZIP file
const zip = new JSZip();
// Add files to the ZIP
zip.file("hello.txt", "Hello World\n");
zip.file("data.json", JSON.stringify({name: "example", value: 42}));
// Create a folder and add a file to it
const imgFolder = zip.folder("images");
imgFolder.file("logo.png", imageData, {base64: true});
// Generate the ZIP file
const content = await zip.generateAsync({type: "blob"});
// For Node.js, generate as buffer
const buffer = await zip.generateAsync({type: "nodebuffer"});
// Load an existing ZIP file
const loadedZip = await JSZip.loadAsync(zipFileData);
const fileContent = await loadedZip.file("hello.txt").async("string");JSZip is built around several key components:
Core functionality for adding, retrieving, and managing files and folders within ZIP archives. Supports hierarchical folder structures and file metadata.
// Add or retrieve files
file(path: string): JSZipObject | null;
file(path: RegExp): JSZipObject[];
file<T extends InputType>(path: string, data: InputByType[T] | Promise<InputByType[T]>, options?: JSZipFileOptions): JSZip;
// Create and navigate folders
folder(name: string): JSZip | null;
folder(name: RegExp): JSZipObject[];
// Remove files or folders
remove(path: string): JSZip;
// Create a deep copy of the ZIP instance
clone(): JSZip;Generate ZIP files in various formats with compression options, progress tracking, and platform-specific optimizations.
// Generate ZIP asynchronously
generateAsync<T extends OutputType>(options?: JSZipGeneratorOptions<T>, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
// Generate as Node.js stream
generateNodeStream(options?: JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
// Generate using internal streaming
generateInternalStream<T extends OutputType>(options?: JSZipGeneratorOptions<T>): JSZipStreamHelper<OutputByType[T]>;Load and parse existing ZIP files from various input sources with validation and error handling capabilities.
// Load ZIP data (static method)
static loadAsync(content: InputFileFormat, options?: JSZipLoadOptions): Promise<JSZip>;
// Load ZIP data (instance method)
loadAsync(data: InputFileFormat, options?: JSZipLoadOptions): Promise<JSZip>;Extract file contents from ZIP archives in various formats with support for streaming and progress tracking.
// JSZipObject methods for content extraction
async<T extends OutputType>(type: T, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;Static properties providing feature detection, version information, and configuration options.
// Static properties
static version: string;
static support: JSZipSupport;
static defaults: object;
static external: { Promise: PromiseConstructorLike };type Compression = 'STORE' | 'DEFLATE';
type InputFileFormat = InputByType[keyof InputByType] | Promise<InputByType[keyof InputByType]>;
interface JSZipSupport {
arraybuffer: boolean;
uint8array: boolean;
blob: boolean;
nodebuffer: boolean;
nodestream: boolean;
}interface InputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
stream: NodeJS.ReadableStream;
}
interface OutputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
nodebuffer: Buffer;
}interface JSZipFileOptions {
base64?: boolean;
binary?: boolean;
date?: Date;
compression?: Compression;
compressionOptions?: CompressionOptions | null;
comment?: string;
optimizedBinaryString?: boolean;
createFolders?: boolean;
dir?: boolean;
dosPermissions?: number | null;
unixPermissions?: number | string | null;
}
interface JSZipLoadOptions {
base64?: boolean;
checkCRC32?: boolean;
optimizedBinaryString?: boolean;
createFolders?: boolean;
decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string;
}
interface JSZipGeneratorOptions<T extends OutputType = OutputType> {
compression?: Compression;
compressionOptions?: CompressionOptions | null;
type?: T;
comment?: string;
mimeType?: string;
encodeFileName?(filename: string): string;
streamFiles?: boolean;
platform?: 'DOS' | 'UNIX';
}interface JSZipObject {
name: string;
unsafeOriginalName?: string;
dir: boolean;
date: Date;
comment: string;
unixPermissions: number | string | null;
dosPermissions: number | null;
options: JSZipObjectOptions;
}
interface JSZipMetadata {
percent: number;
currentFile: string | null;
}
type OnUpdateCallback = (metadata: JSZipMetadata) => void;