extract-zip

2.0.1 · active · verified Sun Apr 19

extract-zip is a pure JavaScript library for asynchronously extracting zip archives into a specified directory. It currently stands at version 2.0.1 and leverages the `yauzl` parser internally to handle zip file structures. Version 2.0.0 marked a significant update, introducing a modern Promise-based API that replaced the older callback-style approach, and adding official TypeScript definitions for enhanced developer experience. The library maintains an active release cadence, with recent updates (like v2.0.1) addressing critical maintenance issues such as the deprecated `process.umask` and clarifying Node.js minimum version requirements. It also offers a command-line interface for direct use. Its primary differentiators include its 100% JavaScript implementation, which avoids native binaries, and its adoption of the modern async/await pattern for all extraction operations, requiring Node.js 10.17.0 or newer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `extract-zip` with its Promise-based API to extract a zip file. It sets up a temporary directory for extraction and includes basic error handling, noting that a real zip file must exist at the specified source path for actual operation.

import extract from 'extract-zip';
import { mkdir, mkdtemp } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';

async function runExtractionDemo() {
  // 1. Define source and target paths.
  //    For a real scenario, 'path/to/your/archive.zip' should be an actual zip file.
  //    For this demo, we'll create a temporary directory for extraction.
  const tempExtractionDir = await mkdtemp(join(tmpdir(), 'extract-zip-demo-'));
  const sourceZipPath = join(__dirname, 'test.zip'); // Assume a 'test.zip' exists alongside this script
                                                     // For a real run, ensure this file exists.
  const targetDirectoryPath = join(tempExtractionDir, 'extracted-content');

  console.log(`Attempting to extract from: ${sourceZipPath}`);
  console.log(`To target directory: ${targetDirectoryPath}`);

  try {
    // Ensure the target directory exists before extraction (optional, extract-zip usually creates it)
    await mkdir(targetDirectoryPath, { recursive: true });

    // 2. Call the extract function.
    await extract(sourceZipPath, { dir: targetDirectoryPath });

    console.log('✅ Extraction complete!');
    console.log(`Content should be in: ${targetDirectoryPath}`);
    // Example: To list extracted files:
    // import { readdir } from 'node:fs/promises';
    // const files = await readdir(targetDirectoryPath);
    // console.log('Extracted files:', files);

  } catch (err: any) {
    console.error('❌ Extraction failed:', err.message);
    if (err.code === 'ENOENT') {
      console.error(`Hint: Make sure '${sourceZipPath}' exists and is a valid zip file.`);
    } else if (err.message.includes('End of Central Directory Record not found')) {
        console.error('Hint: The file might be corrupted or not a valid zip archive.');
    }
  } finally {
    // 3. Clean up the temporary directory. Uncomment for automatic cleanup.
    // console.log(`Cleaning up temporary directory: ${tempExtractionDir}`);
    // await rm(tempExtractionDir, { recursive: true, force: true });
    console.log(`Temporary directory created at: ${tempExtractionDir}. Please inspect and remove manually if needed.`);
  }
}

runExtractionDemo().catch(console.error);

view raw JSON →