Socket
Socket
Sign inDemoInstall

anzip

Package Overview
Dependencies
4
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    anzip

Node Async Unzip Lib


Version published
Weekly downloads
336
decreased by-37.2%
Maintainers
1
Install size
154 kB
Created
Weekly downloads
 

Readme

Source

Anzip

Build Status codecov NPM version License

Anzip is a library to unzip file archive for Node using only one async function.

Purpose

  • Simple to use
  • Modern ES6+ syntax (import instead of require, await/async, ...)
  • Unzip by Yauzl
  • Follows Node best practices

Requirements

  • Node >= 10

Install

yarn add anzip

Or using npm

npm add anzip

Usage

Now that ESM support is widely common, require should be forgotten.

import anzip from 'anzip';

Extract file.zip to current path

await anzip('file.zip');

Extract file.zip to the current path and get output

const output = await anzip('file.zip');
console.log('duration=', output.duration);
console.log('number of files=', output.files.length);

Extract only README.md from file.zip to current path

const output = await anzip('file.zip', { pattern: 'README.md', });
console.log('duration=', output.duration);
console.log('number of files=', output.files.length); // Should be one

Extract only README.md from file.zip to output content variable

const output = await anzip('file.zip', { pattern: 'README.md', outputContent: true });
console.log('duration=', output.duration);
console.log('content=', output.files[0].content);

Extract only README.md from file.zip to output content variable and currentpath

const output = await anzip('file.zip', { pattern: 'README.md', outputPath: './', outputContent: true });
console.log('duration=', output.duration);
console.log('content=', output.files[0].content);

Extract with an entryHandler to fliter entry

const outputPath = './path';
const entryHandler = async entry => {
  let resp = true;
  const fn = entry.name;
  if (fn.endsWith('dummy.pdf')) {
    try {
      await entry.saveTo(outputPath);
      resp = false;
    } catch (e) {
      //
    }
  }
  return resp;
};
const output = await anzip('file.zip',
    outputPath,
    { pattern: /(^(?!\.))(.+(.png|.jpeg|.jpg|.svg|.pdf|.json))$/i,, outputPath: './', entryHandler, outputContent: true });
console.log('duration=', output.duration);
// ./path/dummy.pdf should be saved

Extract using 2 rules and an entryHandler in one to fliter entry

const outputPath = './path';
const entryHandler = async entry => {
  let resp = true;
  const fn = entry.name;
  if (fn.endsWith('dummy.pdf')) {
    try {
      await entry.saveTo(outputPath);
      resp = false;
    } catch (e) {
      //
    }
  }
  return resp;
};
const output = await anzip('file.zip',
    outputPath,
    pattern: /(^(?!\.))(.+(.png|.jpeg|.jpg|.svg|.pdf|.json))$/i,
    flattenPath: true,
    disableSave: true,
    disableOutput: true,
    rules: [
      { pattern: /(^(?!\.))(test.json)$/i, outputContent: true },
      { pattern: /(^(?!\.))(.+(.png|.jpeg|.jpg|.svg|.pdf))$/i, entryHandler },
    ],
  });
console.log('duration=', output.duration);
// ./path/dummy.pdf should be saved

Documentation

One function to rule them all.

output = await anzip(filename, {opts})

Function properties

parameterstypedescription
filenamemandatory stringcontaining zip path to + file
optsoptional objectcontaining optional parameters
opts.outputPathoptional stringthe directory where to to save extracted files
opts.outputContentoptional booleanif set to true, return file.content a Buffer containing file's content
opts.disableSaveoptional booleanif set to true, don't save files
opts.patternoptional regexif set only extract/proceed matching filenames
opts.flattenPathoptional booleanif set don't recreate zip's directories, all file are saved in outputPath
opts.disableOutputoptional booleanif set don't write files to output
opts.entryHandleroptional promiseuse it to add some extra processing to an entry, return true if stream is consumed otherwise false
opts.rulesoptional arrayuse it to add some fine tuned control how to handle each files
opts.rules.patternmandatory regexif it match entry will use rule's parameters instead of global's one

Returned output is an object containing:

parameterstypedescription
durationnumberhow long it took to extract in seconds
filesarrayall files extracted or handled, otherwise empty
files[x].namestringthe filename
files[x].directorystringthe directory in archive (even if opts.flattenPath=true)
files[x].savedbooleantrue if the file was saved to outputPath
files[x].contentBufferthe content of the file available if opts.outputContent=true or rule.outputContent=true
files[x].errorErrorif an error occured

Contribution

Read Contributing Guide for development setup instructions.

FAQs

Last updated on 17 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc