npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

szip

v1.0.3

Published

An event based 7zip wrapper for node

Downloads

6

Readme

Szip

An event based 7zip wrapper for nodejs.

Installation

npm i szip

Events

Most of the functions in this package are event based. Here are some events which you have to use to get the data while in operation.

EVENT.PROGRESS

This event receives how much operation has been done in %.

const { SevenZip, EVENT } =  require("szip");

// Instance created
const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
	console.log(`Done :: ${data}%`);
});

EVENT.FINISH

This event receives when operation finished.

const { SevenZip, EVENT } =  require("szip");

// Instance created
const szip = new SevenZip();

szip.on(EVENT.FINISH, (data) => {
	// 	data.err {string | number} contains error message
	// 	data.buffer {string} containes stdout buffer of the operation
	// 	data.payload {object} contains any information about result.
});

Basic Execution function

By using this function you can execute any 7zip cli commands.

  • run()
  • run_async()

Example run()

const { SevenZip, EVENT } = require("szip");

const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
    console.log(`Done :: ${data}%`);
});

szip.on(EVENT.FINISH, (data) => {
    if (data.err) {
        console.log(data.err);
    } else {
        console.log(data.payload);
        console.log(`Successfull Operation. Exit Code :: ${data.err}`);
    }
});

szip.run(`t "index.zip"`);

Example run_async()

const { SevenZip, EVENT } = require("szip");

const szip = new SevenZip();

szip.run_async(`t "./index.zip"`).then((data) => {
    console.log(data);
}).catch((err) => {
    console.log(err);
});

Note: You don't have to put binary name into args parameter.

High Level functions

These functions are created for easier implementation of 7zip. Currently package have these operations in this category,

  • Extract
  • Add Archive
  • Genarate Hash
  • Test Integrity of Archive

Extract File

To extract a file you have use extract function from sevenzip class. Here is an example below,

const { SevenZip, EVENT } =  require("szip");

const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
	console.log(`Done :: ${data}%`);
});

szip.on(EVENT.FINISH, (data) => {
	if (data.err) {
		console.log(data.err);
	} else {
		console.log(`Successfull Operation. Exit Code :: ${data.err}`);
	}
});

// For normal archives
szip.extract("h.7z", "./extract/*");

// Use option for encrypted archives
szip.extract("h.7z", "./extract/*", {password:  "123@password"});

Create Archive

To create an archive you have to use create.{archivesType}() List of supported archive

  • 7z - create.sevenz() or create.sevenz_async()
  • zip - create.zip() or create.zip_async()
  • gz - create.gzip() or create.gzip_async()
  • bzip2 - create.bzip2() or create.bzip2_async()
  • tar - create.tar() or create.tar_async()
  • wim - create.wim() or create.wim_async()
  • xz - create.xz() or create.xz_async()

Note: Each function have different option parameters because each archive types doesn't support every features like pasword, encrpyted file name or maybe compression method etc.

Example of creating an archive using password + encrypted filenames

const { SevenZip, EVENT } =  require("szip");

const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
	console.log(`Done :: ${data}%`);
});

szip.on(EVENT.FINISH, (data) => {
	if (data.err) {
		console.log(data.err);
	} else {
		console.log(`Successfull Operation. Exit Code :: ${data.err}`);
	}
});

szip.create.sevenz("h.7z", "./*", {
	compression_method: SevenZ_Method.LZMA2,
	compression_level: SevenZ_Level.MAXIMUM,
	encrypt: true,
	encrypt_name: true,
	password: "hello",
});

Generate Hash of File

To genarate hash of file, you have to use hash() or hash_async()

Example hash()

const { SevenZip, EVENT, Hasher } =  require("szip");

const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
	console.log(`Done :: ${data}%`);
});

szip.on(EVENT.FINISH, (data) => {
	if (data.err) {
		console.log(data.err);
	} else {
		console.log(data.payload.hash);
		console.log(`Successfull Operation. Exit Code :: ${data.err}`);
	}
});

szip.hash("index.js", Hasher.SHA1);

Output

Done :: 100%
{ value: '38EF913D283D71177BFCC43828314208A8DB2B66' }
Successfull Operation. Exit Code :: 0

Example hash_async()

const { SevenZip, Hasher } =  require("szip");

const szip = new SevenZip();

szip.hash_async("index.js", Hasher.SHA1).then((data) => {
	console.log(data);
}).catch((err) => {
	console.log(err);
});

Output

{ value: '38EF913D283D71177BFCC43828314208A8DB2B66' }

Note: You can use different hashing alorithms too. Avaliable hashing algorithms are down below.

  • CRC32
  • CRC64
  • SHA1
  • SHA256
  • ALL

Choose any one option from above into the parameter. Hasher.{HashingAlogrithm} By default function will use SHA1 as hashing algorithm if you dont pass hash parameter.

Test integrity of a file

To test integrity of a file, you have to use test() or test_async()

Example of test()

const { SevenZip, EVENT } =  require("szip");

const szip = new SevenZip();

szip.on(EVENT.PROGRESS, (data) => {
	console.log(`Done :: ${data}%`);
});

szip.on(EVENT.FINISH, (data) => {
	if (data.err) {
		console.log(data.err);
	} else {
		console.log(data.payload);
		console.log(`Successfull Operation. Exit Code :: ${data.err}`);
	}
});

szip.test("index.js");

Example of test_async()

const { SevenZip } =  require("szip");

const szip = new SevenZip();

szip.test_async("index.js").then((data) => {
	console.log(data);
}).catch((err) => {
	console.log(err);
});

Very Important Note

Do not run more than one event based function within same instance at the same time, because all the event based function emits into same event into same instance. But you can run multiple non event based functions (like async ones) within same instance at the same time.

To avoid this problem create different instance and then call function from separate instances.

At last

If you like this wrapper package please give this repo a star :)

Credits, 7zip 7zip-bin (npm package)