or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

archive-creation.mdarchive-manipulation.mdarchive-reading.mdcomments-metadata.mdfile-extraction.mdindex.md
tile.json

tessl/npm-adm-zip

JavaScript implementation of ZIP compression and decompression for Node.js with support for in-memory and disk operations

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/adm-zip@0.5.x

To install, run

npx @tessl/cli install tessl/npm-adm-zip@0.5.0

index.mddocs/

ADM-ZIP

ADM-ZIP is a pure JavaScript implementation of ZIP file compression and decompression for Node.js environments. It provides comprehensive ZIP archive functionality including reading existing archives, creating new ones, extracting files, and manipulating archive contents, all without requiring external native dependencies.

Package Information

  • Package Name: adm-zip
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install adm-zip

Core Imports

const AdmZip = require("adm-zip");

For ES modules (with transpilation):

import AdmZip from "adm-zip";

Basic Usage

const AdmZip = require("adm-zip");

// Reading existing archives
const zip = new AdmZip("./archive.zip");
const zipEntries = zip.getEntries();

zipEntries.forEach(function(zipEntry) {
    console.log(zipEntry.entryName); // file path within archive
    if (zipEntry.entryName === "data.txt") {
        console.log(zipEntry.getData().toString("utf8"));
    }
});

// Creating new archives
const newZip = new AdmZip();
newZip.addFile("hello.txt", Buffer.from("Hello World", "utf8"));
newZip.addLocalFile("/path/to/file.pdf");
newZip.writeZip("./output.zip");

// Extracting archives
zip.extractAllTo("./extracted/", true); // overwrite existing files

Architecture

ADM-ZIP is built around several key components:

  • AdmZip Constructor: Main entry point that creates zip instance from file, buffer, or empty
  • ZipEntry Objects: Represent individual files/folders within archives with metadata and content access
  • Archive Operations: Reading, writing, extraction, and manipulation of ZIP archives
  • File System Integration: Support for both in-memory operations and disk I/O
  • Electron Support: Custom file system integration via options for Electron applications

Capabilities

Archive Reading

Read and inspect existing ZIP archives, access file metadata, and extract individual entries or complete archives.

/**
 * Create AdmZip instance from existing archive
 * @param {string|Buffer|object} input - File path, buffer data, or options object
 * @param {object} [options] - Configuration options
 */
function AdmZip(input, options);

/**
 * Get all entries in the archive
 * @param {string} [password] - Password for encrypted archives
 * @returns {ZipEntry[]} Array of ZipEntry objects
 */
getEntries(password);

/**
 * Get specific entry by name
 * @param {string} name - Entry name/path
 * @returns {ZipEntry|null} ZipEntry object or null if not found
 */
getEntry(name);

/**
 * Read file content from archive
 * @param {string|ZipEntry} entry - Entry name or ZipEntry object
 * @param {string|Buffer} [pass] - Password for encrypted files
 * @returns {Buffer|null} File content as Buffer or null if not found
 */
readFile(entry, pass);

/**
 * Read file content asynchronously
 * @param {string|ZipEntry} entry - Entry name or ZipEntry object
 * @param {function} callback - Callback function (err, data)
 */
readFileAsync(entry, callback);

/**
 * Read file content as text
 * @param {string|ZipEntry} entry - Entry name or ZipEntry object
 * @param {string} [encoding="utf8"] - Text encoding
 * @returns {string} File content as string
 */
readAsText(entry, encoding);

/**
 * Read file content as text asynchronously
 * @param {string|ZipEntry} entry - Entry name or ZipEntry object
 * @param {function} callback - Callback function (err, text)
 * @param {string} [encoding="utf8"] - Text encoding
 */
readAsTextAsync(entry, callback, encoding);

/**
 * Get total number of entries in archive
 * @returns {number} Number of entries
 */
getEntryCount();

/**
 * Get number of child entries for a directory
 * @param {string|ZipEntry} entry - Directory entry
 * @returns {number|undefined} Number of child entries
 */
childCount(entry);

/**
 * Iterate over all entries with callback
 * @param {function} callback - Callback function for each entry
 */
forEach(callback);

/**
 * Test archive integrity
 * @param {string|Buffer} [pass] - Password for encrypted archives
 * @returns {boolean} True if archive is valid
 */
test(pass);

Archive Reading

Archive Creation

Create new ZIP archives, add files and folders from disk or memory, and configure compression settings.

/**
 * Add file content to archive
 * @param {string} entryName - File path within archive
 * @param {Buffer|string} content - File content as buffer or string
 * @param {string} [comment] - Optional file comment
 * @param {number|object} [attr] - File attributes or permissions
 * @returns {ZipEntry} Created entry object
 */
addFile(entryName, content, comment, attr);

/**
 * Add local file from disk
 * @param {string} localPath - Path to local file
 * @param {string} [zipPath] - Path within archive
 * @param {string} [zipName] - Custom filename in archive
 * @param {string} [comment] - Optional file comment
 */
addLocalFile(localPath, zipPath, zipName, comment);

/**
 * Add entire local directory
 * @param {string} localPath - Path to local directory
 * @param {string} [zipPath] - Path within archive
 * @param {RegExp|function} [filter] - File filter function or regex
 */
addLocalFolder(localPath, zipPath, filter);

/**
 * Add local file asynchronously
 * @param {object} options - Options object with localPath, zipPath, zipName, comment
 * @param {function} callback - Callback function (err, result)
 */
addLocalFileAsync(options, callback);

/**
 * Add local folder asynchronously
 * @param {string} localPath - Path to local directory
 * @param {function} callback - Callback function (err, result)
 * @param {string} [zipPath] - Path within archive
 * @param {RegExp|function} [filter] - File filter function or regex
 */
addLocalFolderAsync(localPath, callback, zipPath, filter);

/**
 * Add local folder asynchronously with options
 * @param {object} options - Options object with localPath, zipPath, filter
 * @param {function} callback - Callback function (err, result)
 */
addLocalFolderAsync2(options, callback);

/**
 * Add local folder with Promise support
 * @param {string} localPath - Path to local directory
 * @param {object} [props] - Additional properties
 * @returns {Promise<AdmZip>} Promise resolving to AdmZip instance
 */
addLocalFolderPromise(localPath, props);

Archive Creation

File Extraction

Extract files and directories from archives to disk with various options for path handling and permissions.

/**
 * Extract single entry to target path
 * @param {string|ZipEntry} entry - Entry name or ZipEntry object
 * @param {string} targetPath - Target extraction directory
 * @param {boolean} [maintainEntryPath] - Keep original directory structure (default: true)
 * @param {boolean} [overwrite] - Overwrite existing files (default: false)
 * @param {boolean} [keepOriginalPermission] - Preserve file permissions (default: false)
 * @param {string} [outFileName] - Custom output filename
 * @returns {boolean} Success status
 */
extractEntryTo(entry, targetPath, maintainEntryPath, overwrite, keepOriginalPermission, outFileName);

/**
 * Extract entire archive to target directory
 * @param {string} targetPath - Target extraction directory
 * @param {boolean} [overwrite] - Overwrite existing files (default: false)
 * @param {boolean} [keepOriginalPermission] - Preserve file permissions (default: false)
 * @param {string|Buffer} [pass] - Password for encrypted archives
 */
extractAllTo(targetPath, overwrite, keepOriginalPermission, pass);

/**
 * Extract entire archive asynchronously
 * @param {string} targetPath - Target extraction directory
 * @param {boolean} [overwrite] - Overwrite existing files (default: false)
 * @param {boolean} [keepOriginalPermission] - Preserve file permissions (default: false)
 * @param {function} callback - Callback function (err, result)
 */
extractAllToAsync(targetPath, overwrite, keepOriginalPermission, callback);

File Extraction

Archive Manipulation

Modify existing archives by updating, deleting, or managing files and folders within ZIP archives.

/**
 * Update existing entry content
 * @param {ZipEntry|string} entry - Entry to update
 * @param {Buffer} content - New content
 */
updateFile(entry, content);

/**
 * Delete entry and optionally its subfolders
 * @param {ZipEntry|string} entry - Entry to delete
 * @param {boolean} [withsubfolders] - Delete subfolders too (default: true)
 */
deleteFile(entry, withsubfolders);

/**
 * Delete single entry without affecting nested entries
 * @param {ZipEntry|string} entry - Entry to delete
 */
deleteEntry(entry);

Archive Manipulation

Archive Output

Generate and write ZIP archives to disk or memory with various output options and formats.

/**
 * Write archive to disk
 * @param {string} targetFileName - Target file path
 * @param {function} [callback] - Optional callback function (err, result)
 */
writeZip(targetFileName, callback);

/**
 * Write archive to disk with Promise support
 * @param {string} targetFileName - Target file path
 * @param {object} [props] - Additional properties
 * @returns {Promise<void>} Promise resolving when write is complete
 */
writeZipPromise(targetFileName, props);

/**
 * Generate archive as Buffer
 * @param {function} [onSuccess] - Success callback
 * @param {function} [onFail] - Failure callback
 * @param {function} [onItemStart] - Item start callback
 * @param {function} [onItemEnd] - Item end callback
 * @returns {Buffer} Archive as Buffer
 */
toBuffer(onSuccess, onFail, onItemStart, onItemEnd);

/**
 * Generate archive as Buffer with Promise support
 * @returns {Promise<Buffer>} Promise resolving to archive Buffer
 */
toBufferPromise();

Comments and Metadata

Manage ZIP archive comments and individual entry metadata for documentation and organization.

/**
 * Add comment to the entire archive
 * @param {string} comment - Archive comment
 */
addZipComment(comment);

/**
 * Get archive comment
 * @returns {string} Archive comment
 */
getZipComment();

/**
 * Add comment to specific entry
 * @param {ZipEntry} entry - Target entry
 * @param {string} comment - Entry comment
 */
addZipEntryComment(entry, comment);

/**
 * Get comment from specific entry
 * @param {ZipEntry} entry - Target entry
 * @returns {string} Entry comment
 */
getZipEntryComment(entry);

Comments and Metadata

Types

/**
 * Main AdmZip constructor options
 */
interface AdmZipOptions {
  /** Disable file sorting */
  noSort?: boolean;
  /** Read entries during load */
  readEntries?: boolean;
  /** Compression method */
  method?: number;
  /** Custom file system module (for Electron support) */
  fs?: object;
  /** Text decoder/encoder options with efs flag support */
  decoder?: {
    encode?: function;
    decode?: function;
    efs?: boolean;
  };
}

/**
 * Individual archive entry
 */
interface ZipEntry {
  /** Full path name within archive */
  entryName: string;
  /** Raw entry name as bytes */
  rawEntryName: Buffer;
  /** Simple filename without path */
  name: string;
  /** Entry comment */
  comment: string;
  /** Whether entry is a directory */
  isDirectory: boolean;
  /** Entry header information */
  header: object;
  /** File attributes */
  attr: number;
  /** Extra field data */
  extra: Buffer;
  /** Extended File System flag */
  efs: boolean;
  
  /** Get entry data as Buffer */
  getData(pass?: string|Buffer): Buffer;
  /** Get entry data asynchronously */
  getDataAsync(callback: function, pass?: string|Buffer): void;
  /** Set entry content data */
  setData(value: Buffer|string): void;
  /** Get compressed data */
  getCompressedData(): Buffer;
  /** Get compressed data asynchronously */
  getCompressedDataAsync(callback: function): void;
  /** Convert entry to JSON object */
  toJSON(): object;
  /** String representation of entry */
  toString(): string;
}