CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jszip

Create, read and edit .zip files with JavaScript in both browser and Node.js environments

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

JSZip

JSZip 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.

Package Information

  • Package Name: jszip
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install jszip

Core Imports

import JSZip from "jszip";

For CommonJS:

const JSZip = require("jszip");

Basic Usage

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

Architecture

JSZip is built around several key components:

  • JSZip Constructor: Main class for creating and manipulating ZIP instances
  • File Operations: Methods for adding, retrieving, and removing files and folders
  • Data Format Support: Handles multiple input/output formats (strings, arrays, buffers, blobs)
  • Compression Engine: DEFLATE and STORE compression algorithms via pako library
  • Streaming Interface: StreamHelper for handling large files and progress tracking
  • Platform Compatibility: Feature detection and polyfills for browser/Node.js differences

Capabilities

File and Folder Operations

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;

File and Folder Operations

ZIP Generation

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]>;

ZIP Generation

ZIP Loading

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

ZIP Loading

File Content Extraction

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;

File Content Extraction

Utility and Configuration

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

Utility and Configuration

Types

Core Types

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

Input/Output Format Types

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

Configuration Types

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

Object Types

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;
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jszip@3.10.x
Publish Source
CLI
Badge
tessl/npm-jszip badge