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

node-libzip

v1.0.4

Published

## Installation

Downloads

3

Readme

node-libzip

Installation

npm install node-libzip

64-bit integers

libzip uses 64-bit integers types in the native code. In the Node.js side, these are represented as a strings. I decided to keep them, because 32-bit integers cannot go over a certain length. In case of file size, an unsigned 32-bit integer can represent up to 4 GB file size, above that, we would have imprecision issues, not only for file sizes, but for file indices as well.

Using unsigned 64-bit integers, gives us the possibility to handle files up to 17.2 EB (or 17.2 million terabytes) in size, which is more than enough for the foreseeable future.

To circumvent that, you can use the jsbi or other library of your choice to deal with these numbers.

Usage

The API is very similar to the original API of libzip. The only difference is that the functions are in camelCase, and some functions like dirAdd were renamed to addDirectory, fileAdd to addFile, etc.

Examples

Creating an archive and adding a file to it

import * as zip from "node-libzip";
import JSBI from "jsbi";

export default async function () {
    const file1 = path.resolve(__dirname, "test.txt");

    const archive = new zip.Archive();
    // it will throw if the file exists
    archive.open(path.resolve(__dirname, "test.zip"), zip.constants.ZIP_CREATE);
    // if the file exists, it will it when we call archive.close(), otherwise it will create it
    // archive.open(path.resolve(__dirname, 'test.zip'), zip.constants.ZIP_CREATE | zip.constants.ZIP_TRUNCATE);
    const src: zip.Source = archive.sourceFile(
        file1,
        "0",
        (await fs.promises.stat(file1)).size
    );
    // returns the index of the added file as a signed 64-bit integer, represented as a string as stated above
    const index = a.addFile(f.path, src, zip.constants.ZIP_FL_ENC_UTF_8);
}

Iterating over the files in an archive

import * as zip from "node-libzip";
import JSBI from "jsbi";

export default async function () {
    const archive = new zip.Archive();
    archive.open(path.resolve(__dirname, "test.zip"), zip.constants.ZIP_RDONLY);
    const length = archive.getNumEntries(0);
    // we create a single Stat class instance to be reused in the loop, avoiding memory waste
    const stat = new zip.Stat();
    for (
        let i = JSBI.BigInt(0); // i = 0
        JSBI.LT(i, entryCount); // i < entryCount
        i = JSBI.add(i, JSBI.BigInt(1)) // i++
    ) {
        // will return the name of the file as a string
        const fileName = archive.getName(i.toString());
        // will fill the stat instance with the information about the file (size, compression method, encryption algorithm etc.)
        archive.statIndex(i.toString(), 0, stat);
        const file: zip.File = archive.openFileByIndex(i.toString(), 0);
        const buf = new Uint8Array(JSBI.toNumber(JSBI.BigInt(stat.size())));
        file.read(buf, stat.size());
    }
}