unzipit.js
unzipit.js is a JavaScript library providing efficient, random-access decompression of ZIP archives in both browser and Node.js environments. Currently stable at version 2.0.1, it offers significant performance advantages, claiming 6x to 25x faster extraction speeds than alternatives like JSZip, coupled with considerably lower memory consumption. A key differentiating feature is its ability to perform partial downloads of ZIP files by leveraging HTTP range requests, allowing selective extraction without fetching the entire archive. The library supports parallel processing through web workers, further enhancing speed. It accepts various input types, including URLs, Blobs, ArrayBuffers, and custom `Reader` implementations, making it versatile for diverse use cases. While designed for ESM, it also provides a CommonJS entry point for Node.js projects, with recent versions (Node >=18) primarily favoring ES Modules.
Common errors
-
TypeError: (0 , unzipit__WEBPACK_IMPORTED_MODULE_0__.unzip) is not a function
cause Incorrect import syntax for named exports, typically attempting a default import for `unzip`.fixUse a named import: `import { unzip } from 'unzipit';`. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require` syntax in an ES Module environment (browser, or Node.js project configured for ESM).fixUse ES Module `import` statements (e.g., `import { unzip } from 'unzipit'`) or ensure your Node.js project is configured for CommonJS (`"type": "commonjs"` in `package.json`). -
Failed to construct 'Worker': Script at 'path/to/unzipit-worker.module.js' cannot be accessed from origin 'null'.
cause The `workerURL` provided to `setOptions` points to an inaccessible or improperly served path for the web worker script.fixEnsure `workerURL` is a valid, publicly accessible URL relative to your web application's origin, or a data URL. Double-check the path to `unzipit-worker.module.js` in your `dist` folder.
Warnings
- breaking Users migrating from CommonJS (`require`) to ES Modules (`import`) or targeting modern Node.js environments (Node 18+) will experience breaking changes in import syntax.
- gotcha When using web workers for parallel processing, the `workerURL` option *must* be correctly set to the path of `unzipit-worker.module.js`.
- gotcha In Node.js environments, `unzipit` cannot directly fetch from URLs without a custom `Reader` or by pre-loading the ZIP content into an `ArrayBuffer`, `SharedArrayBuffer`, or `TypedArray`.
- gotcha The `unzip` function returns a `Zip` object with an `entries` property, which is a plain object mapping entry names to `ZipEntry` objects. Directly iterating over `entries` as an array will fail.
Install
-
npm install unzipit -
yarn add unzipit -
pnpm add unzipit
Imports
- unzip
import unzip from 'unzipit';
import { unzip } from 'unzipit'; - setOptions
const { setOptions } = require('unzipit');import { setOptions } from 'unzipit'; - * as unzipit (ESM)
const unzipit = require('unzipit');import * as unzipit from 'unzipit';
- unzipit (CommonJS)
const unzipit = require('unzipit');
Quickstart
import { unzip } from 'unzipit';
async function processZipFromUrl(zipUrl: string) {
try {
// Replace with a real URL for actual testing
const dataUrl = zipUrl || 'https://greggman.github.io/unzipit/test/zips/archive-with-folder.zip';
console.log(`Attempting to unzip from: ${dataUrl}`);
const { entries, zip } = await unzip(dataUrl);
console.log(`Total entries found: ${Object.keys(entries).length}`);
// Iterate through all entries and log their names and sizes
for (const [name, entry] of Object.entries(entries)) {
console.log(` Entry: ${name}, Size: ${entry.size} bytes`);
}
// Example: Read a specific file (e.g., 'folder/file1.txt' from the example URL)
const specificEntry = entries['folder/file1.txt'];
if (specificEntry) {
console.log(`\nAttempting to read 'folder/file1.txt' as text...`);
const textContent = await specificEntry.text();
console.log(`Content of 'folder/file1.txt':\n---\n${textContent.substring(0, 100)}...\n---`); // Log first 100 chars
} else {
console.log(`\nEntry 'folder/file1.txt' not found (check URL or entry name).`);
}
// Example: Read another entry as a Blob (e.g., if there was an image.png)
const imageEntry = entries['image.png']; // Replace with an actual image entry if present
if (imageEntry) {
console.log(`\nAttempting to read 'image.png' as a Blob...`);
const imageBlob = await imageEntry.blob('image/png');
console.log(`'image.png' read as Blob of type: ${imageBlob.type}, size: ${imageBlob.size}`);
// In a browser, you could create an object URL: URL.createObjectURL(imageBlob);
} else {
console.log(`\nEntry 'image.png' not found.`);
}
} catch (error) {
console.error("Error processing zip file:", error);
}
}
// Call the function with an example URL. Pass an empty string to demonstrate 'not found' messages.
processZipFromUrl('https://greggman.github.io/unzipit/test/zips/archive-with-folder.zip');