Module that decompresses zip files

Overview

decompress-zip Build Status Coverage Status

Extract files from a ZIP archive

Usage

.extract(options)

Extracts the contents of the ZIP archive file.

Returns an EventEmitter with two possible events - error on an error, and extract when the extraction has completed. The value passed to the extract event is a basic log of each file and how it was compressed.

Options

  • path String - Path to extract into (default .)
  • follow Boolean - If true, rather than create stored symlinks as symlinks make a shallow copy of the target instead (default false)
  • filter Function - A function that will be called once for each file in the archive. It takes one argument which is an object containing details of the file. Return true for any file that you want to extract, and false otherwise. (default null)
  • strip Number - Remove leading folders in the path structure. Equivalent to --strip-components for tar.
  • restrict Boolean - If true, will restrict files from being created outside options.path. Setting to false has significant security implications if you are extracting untrusted data. (default true)
var DecompressZip = require('decompress-zip');
var unzipper = new DecompressZip(filename)

unzipper.on('error', function (err) {
    console.log('Caught an error');
});

unzipper.on('extract', function (log) {
    console.log('Finished extracting');
});

unzipper.on('progress', function (fileIndex, fileCount) {
    console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
});

unzipper.extract({
    path: 'some/path',
    filter: function (file) {
        return file.type !== "SymbolicLink";
    }
});

If path does not exist, decompress-zip will attempt to create it first.

.list()

Much like extract, except:

  • the success event is list
  • the data for the event is an array of paths
  • no files are actually extracted
  • there are no options
var DecompressZip = require('decompress-zip');
var unzipper = new DecompressZip(filename)

unzipper.on('error', function (err) {
    console.log('Caught an error');
});

unzipper.on('list', function (files) {
    console.log('The archive contains:');
    console.log(files);
});

unzipper.list();

License

MIT © Bower team

Comments
  • Tests refactoring

    Tests refactoring

    As conversation in https://github.com/bower/decompress-zip/pull/32 went, here are refactored unit tests.

    Main improvements:

    1. Zip files are stored inside this repo
    2. There are as small amount of test files as possible
    3. Tests are runnable on Windows

    The idea: Next to test package we are also storing recipie how to build this zip package, and explanation what it tests. In current tests there is no info what which zip file actually tries to test (what is confusing as hell). Every test pack contains also json file which specs content inside the zip file, so we have one object to test uncompressed files against. For testing I'm using my own library because it is perfect fit for this job. The lib is quite young still, but I give lifetime guarantee ;).

    Code coverage: There were many files used with previous tests, but all of them were collected in manner "just put together couple of common archives and call it a day", so to my surprise I achieved higher code coverage with only two, better designed zip files.

    Coverage of old tests:

    Statements   : 77.13% ( 253/328 )
    Branches     : 65.52% ( 57/87 )
    Functions    : 67.19% ( 43/64 )
    Lines        : 77.13% ( 253/328 )
    

    Coverage after refactoring:

    Statements   : 78.77% ( 256/325 )
    Branches     : 57.47% ( 50/87 )
    Functions    : 71.88% ( 46/64 )
    Lines        : 78.77% ( 256/325 )
    

    So I stopped with only those two zips. If you want to raise code coverage higher we need to know what we actually have to test. Just throwing next zips on the pile as previous tests shows is not very effective.

    opened by szwacz 9
  • Large zips throw EMFILE (too many file descriptor) errors

    Large zips throw EMFILE (too many file descriptor) errors

    @wibblymat: This pull request fixes #24 ("Unable to decompress relatively big ZIP on Windows"). I think it will occur on linux/mac systems, not just Windows (though I saw it on Windows as well). In my case, I was working with a 56mb zip with lots of files in it and was consistently getting EMFILE errors.

    Switching to https://github.com/isaacs/node-graceful-fs (one of the top 100 depended-on modules in npm) fixed the problem.

    opened by prust 8
  • don't use graceful-fs

    don't use graceful-fs

    What if I don't want to monkey patch fs? I have no choice if this module depends on graceful-fs. Worse, if another module depends on decompress-zip, and I depend on that module, I now am (possibly unknowingly) monkey patching fs.

    opened by andrewrk 5
  • close file after decompression

    close file after decompression

    Just want to ask about closing a zip file after decompression, what was the reason to not close the opened file? Right now the file cannot be deleted, moved or renamed after decompression until the process exit. Is it on purpose or a bug?

    opened by 0vaz 5
  • decompress-zip doesn't preserve permissions

    decompress-zip doesn't preserve permissions

    E.g.

    node -e "var DecompressZip = require('decompress-zip'), unzipper = new DecompressZip('blah.zip'); unzipper.extract({ path: './blah' });"

    My executable files in ./blah are rw-r--r--, should be rwxr-xr-x

    opened by jayproulx 4
  • issue when installing

    issue when installing

    npm ERR! Failed to parse json npm ERR! Unexpected end of input npm ERR! File: /Users/Ilan/.npm/decompress-zip/0.0.4/package/package.json npm ERR! Failed to parse package.json data. npm ERR! package.json must be actual JSON, not just JavaScript. npm ERR! npm ERR! This is not a bug in npm. npm ERR! Tell the package author to fix their package.json file. JSON.parse

    opened by ilanbiala 4
  • Exception when using strip:1

    Exception when using strip:1

    decompress-zip:0.0.6: Extracting a simple zip file with one level of empty directory at the root will throw an TypeError on path.join decompress-zip.js:259 if strip:1 is set

    DecompressZip.prototype.extractFile = function (file, options) {
    var destination = path.join(options.path, file.path);
    

    The issue is in decompress-zip.js:109..120, which, instead of adding .path to the file entry, is returning the path directly in files.map().

    files = files.map(function (file) {
                    if (file.type !== 'Directory') {
    ...
                        return file.path //<-- this is replacing file with file.path in the map.
                        // should be:
                        //   file.path = path.join(dir.join(path.sep), filename);
                        //   return file
    
                    }
    

    Note: The current tests do not detect the issue as they do not validate the feature at all - they just test strip to see if an exception is thrown for the 'stripping too deep' case. Ideally, the stripping too deep test case should validate the exact exception expected and a new test case with strip:1 should be added.

    opened by georgzoeller 3
  • Performance improvements

    Performance improvements

    Decompress zip is slow. I mean, like, really slow.

    ❯ time bower install extjs=http://cdn.sencha.com/ext/commercial/ext-4.2.1-commercial.zip
    bower not-cached    http://cdn.sencha.com/ext/commercial/ext-4.2.1-commercial.zip#*
    bower resolve       http://cdn.sencha.com/ext/commercial/ext-4.2.1-commercial.zip#*
    bower download      http://cdn.sencha.com/ext/commercial/ext-4.2.1-commercial.zip
    bower extract       extjs#* ext-4.2.1-commercial.zip
    bower resolved      http://cdn.sencha.com/ext/commercial/ext-4.2.1-commercial.zip#e-tag:5e5ef2426
    
    real    46m37.575s
    user    46m12.850s
    sys 2m13.090s
    

    For reference:

    ❯ time unzip ext-4.2.1-commercial.zip
    
    real    0m4.470s
    user    0m2.190s
    sys 0m1.000s
    
    opened by wibblymat 3
  • Restrict file extraction to the target path

    Restrict file extraction to the target path

    Currently decompress-zip will extract files outside of the scope of the specified target directory. This has significant security implications when decompressing files from untrusted users.

    This pull request aims to fix this issue by ensuring that the destination path is not be outside set path. \

    A new unit test has also been added to verify this functionality. The test archive has been taken from https://github.com/snyk/zip-slip-vulnerability/tree/master/archives

    opened by Muelsy 2
  • Added progress events, w/ test

    Added progress events, w/ test

    We needed to give the user progress notifications when extracting large zips, so they don't give up or think it is stuck. It's a small change, but may prove useful for others.

    I'm not super-familiar with the codebase or with promises, but I think this should do the trick. I added a test & it passes.

    Note that if #37 reduces the number of files in the first test zip file (as requested in https://github.com/bower/decompress-zip/pull/32#issuecomment-55375832 ?), then the numTotalFiles variable in the test will need to be adjusted.

    opened by prust 2
  • ENOENT bug - Directories not created

    ENOENT bug - Directories not created

    @wibblymat: this fixes #29, which @omnidan first reported and @BCooper63 found the root cause of.

    I needed a patch, so I did the easy/simple thing, which was to expose the cache on the extractors module and clear it out every time extract() is called. But I don't really think it's the correct solution. I suppose the most robust, correct solution would be to attempt the mkdir every time (and catch the error if it fails due to already being there) b/c the directory could hypothetically be deleted at any time by any other process. But that's probably a bit over-the-top, the more practical and expected behavior would be for the cache to be tied to the decompress-zip instance, as @BCooper63 suggests, instead of global to the module, as it is currently. I didn't implement it this way b/c it would've require a deeper refactoring & I wasn't sure if that's the direction you guys want to go with this and, if so, how exactly you would want to implement it (pass the instance's cache to the extractors module?)

    At any rate, this pull request fixes the bug in a minimal way for us, but could cause bugs, for instance if two decompress-zip instances are fired up, one just after the other, the second one, when instantiated, will clear out the global cache and could potentially mess up the first one that is in the middle of running...

    opened by prust 2
  • Decompressing on MacOS node v17.0.1 creates file that fails to execute

    Decompressing on MacOS node v17.0.1 creates file that fails to execute

    I have tested using the following:

    import DecompressZip from 'decompress-zip'
    import extract from 'extract-zip'
    import path from 'path'
    
    extract('ngrok.zip', { dir: path.resolve('~/ezip')}).then(_ => console.log('DONE'))
    
    var unzipper = new DecompressZip('ngrok.zip')
    
    unzipper.on('error', function (err) {
        console.log('Caught an error');
    });
    
    unzipper.on('extract', function (log) {
        console.log('Finished extracting');
    });
    
    unzipper.on('progress', function (fileIndex, fileCount) {
        console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
    });
    
    unzipper.extract();
    

    The ngrok in ./ezip/ngrok works but the one extracted using decompress-zip fails results in:

    ./ngrok
    [1]    52121 killed     ./ngrok
    

    I'm on MacOS 11.6 (Big Sur)

    opened by nsainaney 0
  • decompressing a unix zip fails if external file attribute is 0

    decompressing a unix zip fails if external file attribute is 0

    We've run into a situation where an archive created on Unix can sometimes create a zip file where the external file attribute is 0. This causes this line to fail since 0 isn't in the list of types. Much like how archives created in Windows type property defaults to 'File', the Unix decompression should as well.

    opened by shadargee1982 3
  • Error: Possibly unsupported ZIP platform type

    Error: Possibly unsupported ZIP platform type

    I'm running into an issue with decompress-zip. It works fine on most of the zip files my server generates for packaging files, but on some of the larger files (nearing or exceeding 4.5 GB) it's spitting out an error message:

    • Possibly unsupported ZIP platform type, 140
    • Possibly unsupported ZIP platform type, 44

    When I try to manual unzip the files, they work fine.

    opened by rlugge 0
  • Added option absolute/relative symlinks

    Added option absolute/relative symlinks

    Sometimes there is a need symlinks to be exactly like they were creating during creation of ZIP file and we don't want symlinks to be absolute always.

    I've created option 'absolute' which leaves default behavior by default, but if there is a need someone could pass 'false' and get relative links.

    One example where this is needed - decompression of Electron based application update on MacOS. Once we extract .app file we wan't to be able to move it around(Desktop, Applications folder, etc..) and if we have absolute symlinks it won't work. But if we have relative symlinks everything works like a charm.

    opened by ipetrovic11 0
  • Symlink issue on MacOS

    Symlink issue on MacOS

    On MacOS there is an issue with symlinks after extract. Paths are absolute and in case that some files are moved they become invalid.

    Example is with packed Electron.app. After extracting all symlinks are absolute and if Electron.app is moved to another folder it becomes corrupted.

    Solution would be to provide option to select between relative and absolute symlink paths. Node.js fs already has great utility for that path.relative.

    opened by ipetrovic11 1
Owner
Bower
A package manager for the web
Bower
yet another zip library for node

yazl yet another zip library for node. For unzipping, see yauzl. Design principles: Don't block the JavaScript thread. Use and provide async APIs. Kee

Josh Wolfe 307 Dec 3, 2022
Create, read and edit .zip files with Javascript

JSZip A library for creating, reading and editing .zip files with JavaScript, with a lovely and simple API. See https://stuk.github.io/jszip for all t

Stuart Knightley 8.6k Jan 5, 2023
Automaticly parses known pocket ips patch resources, scans folders or zip files for matching roms and applies the patches.

Pocket Automaton Automaticly parses known pocket ips patch resources, scans folders or zip files for matching roms and applies the patches. Usage pock

null 3 Nov 27, 2022
Pack all your node_modules and other files you want inside your project to a zip file.

?? Node Modules Packer Use Cases | Usage | Examples | Headless | Benchmarks | Reference This is a library to package all your node_modules and other f

Vinicius Lourenço 14 Dec 1, 2022
yet another zip library for node

yazl yet another zip library for node. For unzipping, see yauzl. Design principles: Don't block the JavaScript thread. Use and provide async APIs. Kee

Josh Wolfe 307 Dec 3, 2022
A JavaScript library to read, write, and merge ZIP archives in web browsers.

Armarius About Armarius is a JavaScript library to read, write, and merge ZIP archives in web browsers. This library mainly focuses on a low memory fo

Aternos 5 Nov 9, 2022
Serve file server with single zip file as file system in Deno.

zipland Serve file server with one-single zip file in Deno. Support zip just zip32 with deflated or uncompressed serving plaintext deflate Examples Yo

Yongwook Choi 18 Nov 2, 2022
An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

Snyk Labs 57 Dec 28, 2022
Fintoc.js ES Module - Use the Fintoc widget as an ES module

Fintoc.js ES Module Use the Fintoc widget as an ES module. Installation Install using npm! (or your favourite package manager) # Using npm npm install

Fintoc 6 May 13, 2022
Template Repository for making your own budder Module. CORE is not included, this is just for the module.

A quick copy of the "How to make your own module" section Check out the official budderAPI repository Template Repository for making your own budder M

Logic 2 Apr 3, 2022
Userland module that implements the module path mapping that Node.js does with "exports" in package.json

exports-map Userland module that implements the module path mapping that Node.js does with "exports" in package.json npm install exports-map Usage co

Mathias Buus 9 May 31, 2022
A module federation SDK which is unrelated to tool chain for module consumer.

hel-micro, 模块联邦sdk化,免构建、热更新、工具链无关的微模块方案 Demo hel-loadash codesandbox hel-loadash git Why hel-micro 如何使用远程模块 仅需要一句npm命令即可载入远程模块,查看下面例子线上示例 1 安装hel-micr

腾讯TNTWeb前端团队 319 Jan 3, 2023
Node module for synchronously and recursively merging multiple folders or collections of files into one folder.

merge-dirs Node module for synchronously and recursively merging multiple folders or collections of files into one folder. Install yarn add @nooooooom

不见月 2 Mar 20, 2022
Webpack is an open-source JavaScript module bundler. This includes basic setup files to help me not redo all the setups for webpack when starting a new project.

Webpack Setup Webpack is an open-source JavaScript module bundler. It is made primarily for JavaScript, but it can transform front-end assets such as

Nemwel Boniface 14 Jun 23, 2022
A Node.js module for processing .aff files.

affutil.js 一个照抄 feightwywx/arcfutil aff模块 的 Node.js模块 in Typescript 可以进行aff格式字符串与Javascript对象之间的相互转换 为Note对象提供了有限的方法 恐狼是神,快去用arcfutil 用法 导入: In Node.j

null 2 Jun 9, 2022
NXPayload Converter is a program that allows you to create boot.dat files from Nintendo Switch payload files (.bin)

?? NXPayload Converter NXPayload Converter is a program that allows you to create boot.dat files from Nintendo Switch payload files (.bin) If you have

Murasaki 24 Dec 22, 2022
Piplup: decompile Playdate Pulp .pdx files back to .json project files

Piplup: decompile Playdate Pulp .pdx files back to .json project files This doesn't work yet: I still need to: convert the graphics (.pdt files) back

null 6 Mar 25, 2022
🌸 A cli can automatically generate files from Excel files.

unxlsx A cli can automatically generate files from Excel files. Why We often need to export some information from XLSX to generate our files, such as

Frozen FIsh 24 Aug 22, 2022
JavaScript library for parsing Dirtywave M8 files, complete with a CLI for interacting with M8 files.

m8-js This repository contains a JavaScript library for parsing Dirtywave M8 files, as well as a CLI for interacting with M8 files. The hopes are not

Jeremy Whitlock 20 Dec 17, 2022