Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uzip-module

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uzip-module

Module version of UZIP.js

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

uzip-module

zlib inflate, deflate, inflateRaw, deflateRaw, as well as simple in memory zip creation and parsing.

This is a ES6 module version of UZIP.js

It's faster than pako in my tests.

Usage

import {
  deflate,
  deflateRaw,
  inflate,
  inflateRaw,
  encode,
  parse,
} from 'uzip-module';

deflate/inflate

compress Uint8Array using deflate algorithm.

Includes header and footer

const compressed = deflate(uint8Array);

or

const compressed = deflate(uint8Array, {level:9});
const uncompressedUint8Array = inflate(compressedUint8Array);

or

const uncompressedUint8Array = inflate(
    compressedUint8Array,
    destinationUint8Array);

deflateRaw, inflateRaw

These take the exact same arguments as inflate and deflate but don't store or expect the header or the footer

encode/parse

encode

Creates a zip file. You pass a JavaScript object of filenames to Uint8Arrays it returns a Uint8Array zip file

const utf8Encoder = new TextEncoder();
const files = {
  'stuff/': utf8Encoder.encode(''),
  'stuff/dog.txt': utf8Encoder.encode('german shepard\n'),
  'stuff/birds/': utf8Encoder.encode(''),
  'stuff/birds/bird.txt': utf8Encoder.encode('parrot\n'),
  'stuff/cat.txt': utf8Encoder.encode('siamese\n'),
  'stuff/long.txt': utf8Encoder.encode(`${new Array(200).fill('compress').join('')}\n`),
}
const zipUint8Array = encode(files);

parse

Does the opposite of encode. Takes a zip Uint8Array and returns a JavaScript object of filenames to Uint8Arrays

Calling parse on the zipUint8Array from the previous example will return the same data seen in files above

const unzippedFiles = parse(files);

You can also call it with an extra true in which case it will just return the filenames and a size and csize for each one. The uncompressed and compressed sizes;

const unzippedFileSizes = parse(files, true);

unzippedFileSizes would have a structure like

{
  "stuff/": {
    "size": 0,
    "csize": 0
  },
  "stuff/dog.txt": {
    "size": 15,
    "csize": 15
  },
  "stuff/long.txt": {
    "size": 1601,
    "csize": 24
  }
}

Notes

All credit goes to the original author Photopea.

I ported this to ES6 modules to use in another library. Originally I thought about putting the various parts in more separate files like the inflate in one file, deflate in another, parse, and encode in others but tree shaking is supposed to handle this stuff more or less so I think just moving it to ES6 modules is enough to get any unused code stripped. The only other think maybe left to do is change the bin and f imports in UZIP.js to import each individual identifier instead of all of them.

Also I spent about an hour trying to get ES6 modules to work with mocha in node but failed so got sick of wasting time and used puppeteer. Patches welcome to remove that dependency.

I didn't make a non-ES6 version. If it's important submit a PR (babel) and I'll take a look.

Keywords

FAQs

Package last updated on 23 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc