yauzl - Node.js Zip File Reader

3.3.0 · active · verified Tue Apr 21

yauzl (yet another unzip library) is a Node.js package designed for reading and extracting `.zip` archives. It strictly adheres to the ZIP specification, differentiating itself by reading the central directory for file metadata rather than scanning for local file headers, which are prone to being out of sync with the central directory. The library emphasizes non-blocking, asynchronous APIs and efficient memory usage, particularly through its `lazyEntries` option, which prevents buffering entire files or all entries into RAM simultaneously. Version 3.3.0 is the current stable release, with the project demonstrating a healthy and positive release cadence, with updates typically occurring every few months. Key differentiators include robust error handling, built-in validation to guard against unsafe file names and potential zip bomb attacks, and support for non-conformant zip files created by certain Microsoft tools. The API is callback-based, making it suitable for event-driven Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to open a zip file, process entries one by one using `lazyEntries`, handle both files and directories, and stream content to disk.

import * as fs from 'fs';
import * as path from 'path';
import yauzl from 'yauzl';

const zipFilePath = 'path/to/your/archive.zip';
const extractDir = 'path/to/extract/to';

fs.mkdirSync(extractDir, { recursive: true });

yauzl.open(zipFilePath, { lazyEntries: true, autoClose: true }, (err, zipfile) => {
  if (err) throw err;

  zipfile.on('entry', (entry) => {
    const entryPath = path.join(extractDir, entry.fileName);
    if (//$/.test(entry.fileName)) {
      // Directory file names end with '/'
      fs.mkdirSync(entryPath, { recursive: true });
      zipfile.readEntry();
    } else {
      // File entry
      zipfile.openReadStream(entry, (err, readStream) => {
        if (err) throw err;
        readStream.on('end', () => {
          zipfile.readEntry();
        });
        // Ensure parent directory exists for the file
        fs.mkdirSync(path.dirname(entryPath), { recursive: true });
        readStream.pipe(fs.createWriteStream(entryPath));
      });
    }
  });

  zipfile.on('end', () => {
    console.log('Zip extraction complete.');
  });

  // Start reading entries
  zipfile.readEntry();
});

view raw JSON →