unzipit.js

2.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `unzipit` to fetch a ZIP file from a URL, list its contents, and extract specific entries as text and Blob objects, handling potential errors.

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

view raw JSON →