Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

7zip-min

onikienko74kMIT2.1.0TypeScript support: included

Standalone cross-platform zip/unzip with 7za

7z, 7zip, 7-zip, 7za, compress, decompress, extract, lzma, zip, gzip, bzip2, tar, pack, unpack

readme

7zip-min

Minimal cross-platform pack/unpack (and any command) with 7-zip for Node.js.
It does not require 7zip to be installed in your system. This package includes a standalone 7za version of 7-Zip (uses precompiled binaries from 7zip-bin package).

Supporting archive formats

According to Command Line Version User's Guide page, 7za supports only 7z, lzma, cab, zip, gzip, bzip2, Z** and **tar formats.

Supporting platforms

  • win (32/64)
  • mac
    • arm64 (Apple Silicon)
    • x64
  • linux
    • arm
    • arm64
    • ia32
    • x64

To get more details, check 7zip-bin package repo.

Package should work with Electron. You will have to unpack the binary (asarUnpack option if you use electron-builder.)

Usage

You may use pack and unpack methods for simple packing/unpacking.

You can also use list to get an array with the file content properties (includes date, time, attr, size, compressed, and name)

Or use cmd to run 7za with custom parameters (see Command Line Version User's Guide)

You can use package with callbacks and in async way (with promises).

Basic examples

const _7z = require('7zip-min');

// .......
await _7z.pack('path/to/dir/or/file', 'path/to/archive.7z');
await _7z.unpack('path/to/archive.7z', 'where/to/unpack');
const list = await _7z.list('path/to/archive.7z'); // list of items inside archive
const output = await _7z.cmd(['a', 'path/to/archive.7z', 'path/to/dir/or/file']); // run custom command

Examples with callbacks and promises

unpack()

// unpack with callback
_7z.unpack('path/to/archive.7z', 'where/to/unpack', (err, output) => {
    if (err) {
        console.error('Error', err.message);
    } else {
        // everything is Ok
        console.log('stdout of the 7za command execution', output); // output just in case you need it
    }
});

// unpack with promise
_7z.unpack('path/to/archive.7z', 'where/to/unpack')
    .then(output => console.log('stdout of the 7za command execution', output))
    .catch(err => console.error('Error', err.message));

// unpack with async/await
(async () => {
    try {
        const output = await _7z.unpack('path/to/archive.7z', 'where/to/unpack');
        console.log('stdout of the 7za command execution', output);
    } catch (err) {
        console.error('Error', err.message);
    }
})();

// if no output directroy specified, it will unpack into the current directory (process.cwd())
_7z.unpack('path/to/archive.7z')
    .then(output => console.log('stdout of the 7za command execution', output))
    .catch(err => console.error('Error', err.message));

pack()

// pack with callback
_7z.pack('path/to/dir/or/file', 'path/to/archive.7z', (err, output) => {
    if (err) {
        console.error('Error', err.message);
    } else {
        // everything is Ok
        console.log('stdout of the 7za command execution', output);
    }
});

// pack with promise
_7z.pack('path/to/dir/or/file', 'path/to/archive.7z')
    .then(output => console.log('stdout of the 7za command execution', output))
    .catch(err => console.error('Error', err.message));

// pack with async/await
(async () => {
    try {
        const output = await _7z.pack('path/to/dir/or/file', 'path/to/archive.7z');
        console.log('stdout of the 7za command execution', output);
    } catch (err) {
        console.error('Error', err.message);
    }
})();

list()

// list with callback
_7z.list('path/to/archive.7z', (err, list) => {
    if (err) {
        console.error('Error', err.message);
    } else {
        console.log('List of items inside the archive', list);
        // For each element in the archive you will have:
        // name, date, time, attr, size (in bytes), compressed (compressed size in bytes), crc, method, encrypted, block
        // Depending on the archive type some values may be empty or missed
    }
});

// list with promise
_7z.list('path/to/archive.7z')
    .then(list => console.log('List of items inside the archive', list))
    .catch(err => console.error('Error', err.message));

// list with async/await
(async () => {
    try {
        const list = await _7z.list('path/to/archive.7z');
        console.log('List of items inside the archive', list);
    } catch (err) {
        console.error('Error', err.message);
    }
})();

cmd()

// cmd with callback
// in the first parameter you have to provide an array of parameters
// check 7z's Command Line Version User's Guide - https://web.mit.edu/outland/arch/i386_rhel4/build/p7zip-current/DOCS/MANUAL/
// the bellow command is equal to `7za a path/to/archive.7z path/to/dir/or/file` and will add `path/to/dir/or/file` to `path/to/archive.7z` archive
_7z.cmd(['a', 'path/to/archive.7z', 'path/to/dir/or/file'], (err, output) => {
    if (err) {
        console.error('Error', err.message);
    } else {
        // Execution finished succesfully
        console.log('stdout of the 7za command execution', output);
    }
});

// cmd with promise
_7z.cmd(['a', 'path/to/archive.7z', 'path/to/dir/or/file'])
    .then(output => console.log('stdout of the 7za command execution', output))
    .catch(err => console.error('Error', err.message));

// cmd with async/await
(async () => {
    try {
        const output = await _7z.cmd(['a', 'path/to/archive.7z', 'path/to/dir/or/file']);
        console.log('stdout of the 7za command execution', output);
    } catch (err) {
        console.error('Error', err.message);
    }
})();

Custom 7za path

Sometimes, you may want to use a custom path to the 7za binary. See https://github.com/onikienko/7zip-min/pull/106 for more details.

// To find out the path to the 7za binary, you can use the getConfig() method
const config = _7z.getConfig();
console.log(`Path to 7za binary: ${config.binaryPath}`); 

// To set a custom path to the 7za binary, you can use the config() method
_7z.config({
    binaryPath: 'path/to/custom/7za'
});

Test

npm test

changelog

Changelog

2.0.0 (2025-01-23)

Features

  • ability to use the same API in async way (e8a9106), closes #24
  • add types definition index.d.ts (578c33a)
  • return stdout/stderr output printed by 7z (a563837), closes #38
  • support electron (app.assar.unpacked) (3f1c867), closes #98

Documentation

  • more detailed jsdoc comments (ad0df9d)
  • update CHANGELOG.md (60da359)
  • update date in LICENSE (a54ab60)
  • update README.md with new ways to use API (b9bb08e)

Build System and Dependencies

  • add "files" prop to package.json (f1c8cc7)
  • add pack and unpack keywords to package.json (bb57846)
  • dev-deps: ava 6.2.0 (a914f9c)
  • dev-deps: fs-extra v11.3.0 (12c061e)
  • dev-deps: globe v11.0.1 and usage in test.js (8ba8aaf)
  • dev-deps: add @types/fs-extra v11.0.4 (4eba204)
  • use release-it instead of np (4dded46)

v1.4.5

  • build: update .gitignore
  • docs: update README.md

v1.4.4

  • fix error with 'list' command if file contains " = " in the name
  • bump dependencies
  • add test to check filename with " = " for list command

v1.4.2

  • bump dependencies
  • update README.md
  • use 2fa for npm releases

v1.4.1

v1.4.0

  • add 7z output to error object (you will have more ideas on why 7z failed)
  • add spawn option {windowsHide: true} (it will hide the subprocess console window that would normally be created on Windows systems.)

Thanks @milahu for that features.

v1.3.3

  • Bump path-parse from 1.0.6 to 1.0.7
  • Bump normalize-url from 4.5.0 to 4.5.1
  • Bump glob-parent from 5.1.1 to 5.1.2

v1.3.2

  • Bump hosted-git-info from 2.8.8 to 2.8.9

v1.3.1

  • Bump 7zip-bin from 5.0.3 to 5.1.1
  • Bump fsify from 4.0.1 to 4.0.2
  • [Security] Bump lodash from 4.17.20 to 4.17.21
  • Bump glob from 7.1.6 to 7.1.7
  • Update package.json
  • [dev] Upgrade to GitHub-native Dependabot

v1.3.0 (2021-01-18)

  • Fix list command for nested 7zip archives
  • Add new props for list output (crc, method, block, encrypted). Props may depend on archive type and platform.
  • Update README.md
  • (dev) Bump ava from v3.14.0 to v3.15.0
  • (dev) Tests with nested folders structure
  • (dev) Test for cmd command

v1.2.1 (2020-12-23)

  • [Security] Bump ini from 1.3.5 to 1.3.7
  • Bump ava from v3.8.1 to v3.14.0

v1.2.0 (2020-10-13)

  • Make output path for .unpack optional (if not provided will unpack in the current dir)
  • Bump ava from 3.12.1 to 3.13.0

v1.1.3 (2020-09-02)

  • Bump ava from 3.8.1 to 3.12.1
  • Bump lodash to 4.17.19

v1.1.2 (2020-05-05)

  • Bump ava from 2.4.0 to 3.8.1

v1.1.1 (2019-10-02)

  • Update dependencies

v1.1.0 (2019-08-30)

  • Added list command

v1.0.1 (2018-10-14)

  • Added CHANGELOG.md file
  • Updated README.md

v1.0.0 (2018-10-14)

  • Added cmd method (running 7za with user defined parameters)
  • jsdoc for methods
  • Updated README.md

v0.0.2 (2018-09-27)

  • Cleanup the code
  • Updated README.md

v0.0.1 (2018-09-27)

  • Initial version with only pack and unpack methods