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

zip-lib

v1.0.5

Published

zip and unzip library for node

Downloads

389,746

Readme

zip-lib

zip and unzip library for node.

npm Package GitHub license node Node.js CI

Install

npm install zip-lib

Quick Start

Zip

You can use zip-lib to compress files or folders.

Zip single file

const zl = require("zip-lib");

zl.archiveFile("path/to/file.txt", "path/to/target.zip").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Zip single folder

const zl = require("zip-lib");

zl.archiveFolder("path/to/folder", "path/to/target.zip").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Unzip

const zl = require("zip-lib");

zl.extract("path/to/target.zip", "path/to/target").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Advanced usage

Sets the compression level

const zl = require("zip-lib");

zl.archiveFolder("path/to/folder", "path/to/target.zip", { compressionLevel: 9 }).then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Zip multiple files and folders

const zl = require("zip-lib");

const zip = new zl.Zip();
// Adds a file from the file system
zip.addFile("path/to/file.txt");
// Adds a folder from the file system, putting its contents at the root of archive
zip.addFolder("path/to/folder");
// Generate zip file.
zip.archive("path/to/target.zip").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

The path/to/folder directory is as follows:

path/to/folder
.
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

And the generated path/to/target.zip archive file directory will be as follows:

path/to/target.zip
.
├── file.txt
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

Zip with metadata

const zl = require("zip-lib");

const zip = new zl.Zip();
// Adds a file from the file system
zip.addFile("path/to/file.txt", "renamedFile.txt");
zip.addFile("path/to/file2.txt", "folder/file.txt");
// Adds a folder from the file system, and naming it `new folder` within the archive
zip.addFolder("path/to/folder", "new folder");
// Generate zip file.
zip.archive("path/to/target.zip").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

The path/to/folder directory is as follows:

path/to/folder
.
├── dir1
│   ├── file.ext
├── dir2
└── file_in_root.ext

And the generated path/to/target.zip archive file directory will be as follows:

path/to/target.zip
.
├── renamedFile.txt
├── folder
│   ├── file.txt
│── new folder
    ├── dir1
    │   ├── file.ext
    ├── dir2
    └── file_in_root.ext

Unzip with entry callback

Using onEntry callback we can know the current progress of extracting and control the extraction operation. See IExtractOptions.

const zl = require("zip-lib");

const unzip = new zl.Unzip({
    // Called before an item is extracted.
    onEntry: function (event) {
        console.log(event.entryCount, event.entryName);
    }
})
unzip.extract("path/to/target.zip", "path/to/target").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Unzip and exclude specified entries

The following code shows how to exclude the __MACOSX folder in the zip file when extracting. See IExtractOptions.

const zl = require("zip-lib");

const unzip = new zl.Unzip({
    // Called before an item is extracted.
    onEntry: function (event) {
        if (/^__MACOSX\//.test(event.entryName)) {
            // entry name starts with __MACOSX/
            event.preventDefault();
        }
    }
})
unzip.extract("path/to/target.zip", "path/to/target").then(function () {
    console.log("done");
}, function (err) {
    console.log(err);
});

Cancel zip

If the cancel method is called after the archive is complete, nothing will happen.

const zl = require("zip-lib");

const zip = new zl.Zip();
zip.addFile("path/to/file.txt");
zip.archive("path/to/target.zip").then(function () {
    console.log("done");
}, function (err) {
    if (err.name === "Canceled") {
        console.log("cancel");
    } else {
        console.log(err);
    }
});

// Cancel zip
zip.cancel();

Cancel unzip

If the cancel method is called after the extract is complete, nothing will happen.

const zl = require("zip-lib");

const unzip = new zl.Unzip();
unzip.extract("path/to/target.zip", "path/to/target").then(function () {
    console.log("done");
}, function (err) {
    if (err.name === "Canceled") {
        console.log("cancel");
    } else {
        console.log(err);
    }
});

// cancel
unzip.cancel();

API

Method: archiveFile

archiveFile(file, zipFile, [options])

Compress a single file to zip.

  • file: String
  • zipFile: String
  • options?: IZipOptions (optional)

Returns: Promise<viod>

Method: archiveFolder

archiveFolder(folder, zipFile, [options])

Compress all the contents of the specified folder to zip.

  • folder: String
  • zipFile: String
  • options?: IZipOptions (optional)

Returns: Promise<void>

Method: extract

extract(zipFile, targetFolder, [options])

Extract the zip file to the specified location.

Returns: Promise<void>

Class: Zip

Compress files or folders to a zip file.

Constructor: new Zip([options])

Method: addFile(file, [metadataPath])

Adds a file from the file system at realPath into the zipfile as metadataPath.

  • file: String
  • metadataPath?: String (optional) - Typically metadataPath would be calculated as path.relative(root, realPath). A valid metadataPath must not start with / or /[A-Za-z]:\//, and must not contain ...

Returns: void

Method: addFolder(folder, [metadataPath])

Adds a folder from the file system at realPath into the zipfile as metadataPath.

  • folder: String
  • metadataPath?: String (optional) - Typically metadataPath would be calculated as path.relative(root, realPath). A valid metadataPath must not start with / or /[A-Za-z]:\//, and must not contain ...

Returns: void

Method: archive(zipFile)

Generate zip file.

  • zipFile: String

Returns: Promise<viod>

Method: cancel()

Cancel compression. If the cancel method is called after the archive is complete, nothing will happen.

Returns: void

Class: Unzip

Extract the zip file.

Constructor: new Unzip([options])

Method: extract(zipFile, targetFolder)

Extract the zip file to the specified location.

  • zipFile: String
  • targetFolder: String

Returns: Promise<void>

Method: cancel()

If the cancel method is called after the extract is complete, nothing will happen.

Returns: void

Options: IZipOptions

Object

  • followSymlinks?: Boolean (optional) - Indicates how to handle when the given path is a symbolic link. The default value is false.true: add the target of the symbolic link to the zip.false: add symbolic link itself to the zip.
  • compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 - Sets the compression level. The default value is 6.0: the file data will be stored.1-9: the file data will be deflated.

Options: IExtractOptions

Object

  • overwrite?: String (optional) - If it is true, the target directory will be deleted before extract. The default value is false.

  • symlinkAsFileOnWindows?: Boolean (optional) - Extract symbolic links as files on Windows. This value is only available on Windows and ignored on other platforms. The default value is true.If true, the symlink in the zip will be extracted as a normal file on Windows.If false, the symlink in the zip will be extracted as a symlink correctly on Windows, but an EPERM error will be thrown under non-administrators.

    WARNING: On Windows, the default security policy allows only administrators to create symbolic links. If you set symlinkAsFileOnWindows to false and the zip contains symlink, be sure to run the code under the administrator, otherwise an EPERM error will be thrown.

  • onEntry?: Function (optional) - Called before an item is extracted.Arguments:

    • event: Object - Represents an event that an entry is about to be extracted.
      • entryName: String (readonly) - Entry name.
      • entryCount: Number (readonly) - Total number of entries.
      • preventDefault(): Function - Prevent extracting current entry. This method can be used to prevent extraction of the current item. By calling this method we can control which items can be extracted.

License

Licensed under the MIT license.