Giter Site home page Giter Site logo

but-unzip's Introduction

but-unzip

small unzip library. ~743 bytes for Node, and ~999^ bytes for browsers.

^90%+ of browsers support the decompression API. For the last 10%, dynamically import pako, adding ~20k.

Usage

Install via your favorite package manager and import but-unzip. Has zero dependencies.

$ npm install but-unzip

# for old browsers you need
$ npm install pako

This library returns zip entries synchronously, but only returns an entry's uncompressed bytes after calling .read(), which'll give Uint8Array or Promise<Uint8Array>.

Naïve use

If there's a built-in function to inflate compressed files (like in Node or 90%+ of browsers), you can use the code like:

import { iter } from 'but-unzip';
import * as fs from 'fs';

const bytes = fs.readFileSync('somezip.zip');

for (const entry of iter(bytes)) {
  console.info(entry.name, entry.comment);
  const bytes = await entry.read();
  // do something with bytes
}

Provide inflate function

If you're worried about maximum compatibility:

import { unzip, inflateRaw as platformInflateRaw } from 'but-unzip';
import { inflateRaw as pakoInflateRaw } from 'pako/lib/inflate.js';

async function decompressUint8Array(zipBytes) {
  const allEntries = unzip(zipBytes, platformInflateRaw || pakoInflateRaw);
  // do something with entries
}

Dynamically import inflate

You should only fetch pako if you need to, because again, 90% of people don't need it:

import { unzip, inflateRaw as platformInflateRaw } from 'but-unzip';

const inflateRaw = platformInflateRaw || (await import('pako/lib/inflate.js').inflateRaw);

// later
const all = unzip(zipBytes, inflateRaw);

Limitations

  • This library doesn't support ZIP64, but probably should. But your browser (and Node) will probably not be happy to work with 4gb+ files, especially as this is not a streaming library (it just gives everything at once).

  • Like literally every zip library that exists, this only supports compression types 0 (store) and 8 (deflate).

Notes

  • Pako's ESM bundling can be a bit broken, so importing 'pako/lib/inflate.js' adds ~20k. Importing 'pako' wholesale, even if you only use inflateRaw, adds ~45k.

  • The main thread is only good for decompressing small things. If you're handling user data and it could be really big, use a Worker.

but-unzip's People

Contributors

samthor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

but-unzip's Issues

TypeScript #-imports supports

I see this comment regarding TypeScript doesn't support #-imports:

// @ts-ignore Node/esbuild supports #-imports but TS does not
import { inflateRaw } from '#default-inflate';

Could you share the issue link for TypeScript?
Thanks in advance!

Publish the package types

Would it be possible to publish the package types?
It would help when using this library in TypeScript.

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.