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

mozip

v1.0.3

Published

Minimal library for generating ZIP files using Promises

Downloads

215

Readme

Mozip

Minimal library for generating ZIP files using Promises

Features

Mozip is for those who don't need any complex things and just want to pack a few files.

  • No dependencies except Node.js built-in modules.
  • Outputs as a Node.js stream.
  • Provides Promise-based API.
  • Supports UTF-8 file names.
  • Supports store (no compression) and deflate compression methods.
  • Supports setting zlib options: compression level, chunk size, etc.
  • Does NOT support comments.
  • Does NOT support Zip64 (files larger than or equal 4 GiB).

Usage

import { Buffer } from 'node:buffer';
import { writeFile } from 'node:fs/promises';
import { ZipStream, dateToDosDateTime } from 'mozip';

const zip = new ZipStream();
const writing = writeFile('example.zip', zip);
const data = Buffer.from('Hello, World!\n', 'utf8');

// Write data
zip.writeFile('uni♥code♦.txt', data);

// Can set compression level
zip.writeFile('best-compression.txt', data, { zlib: { level: 9 } });
zip.writeFile('no-compression.txt', data, { compress: false });

// Can set modified date/time
const date = new Date(2001, 0, 1);
zip.writeFile('20010101/0.txt', data, { lastModified: date });

// Can reuse options with pre-calculated date/time
const options = { lastModified: dateToDosDateTime(date) };
zip.writeFile('20010101/1.txt', data, options);
zip.writeFile('20010101/2.txt', data, options);

// Can handle errors that occurred before writing data into stream
await zip.writeFile('../invalid-filename/', data).catch(() => {
  console.warn('The stream was not destroyed. Just ignore it.');
});

// Must be ended
zip.end();
await writing;

Install

Mozip is published in the npm registry as mozip. Install using your package manager (e.g. npm install mozip), or download a tarball/ZIP file from the latest release.

API

new ZipStream()

The ZipStream class inherits the Transform class of the node:stream module. All parameters when constructing are ignored.

ZipStream.prototype.writeFile(name, data[, options])

Add a file to entries and push its data to the stream. Duplicated file names in entries are not allowed.

Parameters:

  • name: string
  • data: TypedArray (includes Buffer of the node:buffer) or DataView
  • options: (optional) object that implements below properties:
    • compress: (optional) boolean
    • lastModified: (optional) Date or number
    • zlib: (optional) object that implements the Options interface of the node:zlib

Returns a Promise that resolves after compressing completed and data pushed to the stream.

The name parameter would be a file name. Absolute paths are not allowed.

Data become compressed by default. To store without compression, set the compress property in the options parameter to false.

If the lastModified property of the options parameter is undefined, it would be a moment when compressing started, according to the ZIP specification. If a number, it must be an unsigned 32-bit integer that represents MS-DOS date and time. Otherwise, it would be converted by the dateToDosDateTime(date) function.

ZipStream.prototype.end()

Ends the stream of a writable side, flushing central directory headers and the end of central directory record.

Returns a Promise that fulfills with a number, total byte length of the generated file.

dateToDosDateTime(date)

Parameters:

  • date: Date or object that implements below methods:
    • getFullYear()
    • getMonth()
    • getDate()
    • getHours()
    • getMinutes()
    • getSeconds()

Returns a number, unsigned 32-bit integer that represents MS-DOS date and time.

Notes

Mozip's name is a pun; a Korean word '모집(mojip)' means collecting, and ZIP's Hangul notation is '집'.

Overall API is inspired by the excellent zipping library yazl made by Josh Wolfe.