Discover the Versatile `unzipper` Library for Effortless JavaScript File Extraction

Introducing Unzipper – The Ultimate JavaScript Library for File Extraction

Looking for an efficient way to handle zip files in your JavaScript applications? Meet unzipper, your go-to library for extracting zip files effortlessly.

Why Choose Unzipper?

Unzipper is a powerful and flexible library designed to simplify the extraction of zip files in JavaScript. Whether you are dealing with large archives or need to extract files on the fly, Unzipper has got you covered.

Getting Started

  
    const unzipper = require('unzipper');
  

Basic Usage

  
    const fs = require('fs');
    const unzipper = require('unzipper');

    fs.createReadStream('path/to/archive.zip')
      .pipe(unzipper.Extract({ path: 'output/path' }))
      .on('close', () => console.log('Extraction complete'));
  

Case Specific Extrations

If you need more control over what and how things are extracted, here are some useful APIs:

Extracting Specific File

  
    fs.createReadStream('path/to/archive.zip')
      .pipe(unzipper.Parse())
      .on('entry', function (entry) {
        const fileName = entry.path;
        const type = entry.type; // 'Directory' or 'File'
        if (fileName === "thisDocument.txt") {
          entry.pipe(fs.createWriteStream('output/path/thisDocument.txt'));
        } else {
          entry.autodrain();
        }
      });
  

Listing File Contents

  
    fs.createReadStream('path/to/archive.zip')
      .pipe(unzipper.Parse())
      .on('entry', function (entry) {
        console.log(entry.path);
        entry.autodrain();
      });
  

Stream Mode

Unzipper supports Node.js streams, making it a great fit for streaming data.

  
    fs.createReadStream('path/to/archive.zip')
      .pipe(unzipper.Parse())
      .on('entry', function (entry) {
        entry.pipe(process.stdout);
      });
  

Promise Mode

If you prefer using promises, Unzipper also supports async/await syntax.

  
    const directory = await unzipper.Open.file('path/to/archive.zip');
    await directory.extract({ path: 'output/path' });
  

A Complete Example Application

Below is a simple application that uses Unzipper to extract a zip file and lists its contents after extraction:

  
    const fs = require('fs');
    const unzipper = require('unzipper');

    async function extractAndList() {
      const directory = await unzipper.Open.file('path/to/archive.zip');
      await directory.extract({ path: 'output/path' });

      directory.files.forEach(file => {
        console.log(file.path);
      });
    }

    extractAndList().catch(err => console.error(err));
  

With the ease, flexibility, and powerful API provided by Unzipper, handling zip files in your JavaScript applications has never been easier!

Hash: ab1f9607034d31797b9fbba436eca36cfbfffa895fee1c68338b0ad318e24e9b

Leave a Reply

Your email address will not be published. Required fields are marked *