extract-zip
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
-
TypeError: extract is not a function
cause Attempting to use `extract` in a CommonJS module with incorrect destructuring or trying to use the old callback API after upgrading to v2.0.0+.fixFor CommonJS, use `const extract = require('extract-zip')`. For all versions >= 2.0.0, ensure you're using the Promise-based API with `await extract(source, options)`. -
Error: Your Node.js version is too old. This module requires Node.js >= 10.17.0.
cause Running `extract-zip` v2.0.1 or later on an unsupported Node.js version.fixUpgrade your Node.js environment to version 10.17.0 or newer. Check the official Node.js website for LTS releases. -
Error: End of Central Directory Record not found
cause The provided source file is either not a valid zip archive or is corrupted, preventing the `yauzl` parser from identifying the central directory.fixVerify that the `source` path points to a legitimate and uncorrupted `.zip` file. Try opening the zip file manually to confirm its integrity.
Warnings
- breaking The callback-style API was entirely removed in version 2.0.0. All operations now return Promises and should be handled with `await` or `.then/.catch`.
- breaking Support for Node.js versions older than 10.12 was dropped in version 2.0.0. The library leverages `fs.promises`, which stabilized in newer Node versions.
- breaking The minimum required Node.js version was corrected to 10.17.0 in version 2.0.1, due to dependencies on `fs.promises` stability. This means installations on Node.js versions between 10.12.0 and 10.16.x that worked with v2.0.0 will now fail with v2.0.1.
- gotcha The `onEntry` option function expects two arguments: `(entry, zipfile)`. The `entry` object is forwarded directly from the `yauzl` library, and `zipfile` is the `yauzl` instance itself. Always ensure `zipfile` is closed when you are done.
Install
-
npm install extract-zip -
yarn add extract-zip -
pnpm add extract-zip
Imports
- extract (ESM)
import { extract } from 'extract-zip'import extract from 'extract-zip'
- extract (CommonJS)
const { extract } = require('extract-zip')const extract = require('extract-zip') - ExtractOptions (TypeScript type)
import { ExtractOptions } from 'extract-zip'import type { ExtractOptions } from 'extract-zip'
Quickstart
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);