Skip to content

bsfranca2/unzip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unzip

Installation

npm install @buh/unzip

Quick Examples

Bun

Use inflateSync from Bun.inflateSync.

import { Zip } from '@buh/unzip';

const file = Bun.file('archive.zip');
const arrayBuffer = await file.arrayBuffer();

const zip = await Zip.create(arrayBuffer);
for (const entry of zip.iterator()) {
  console.log(entry.fileName, entry.lastModified(), entry.getData());
}

Node

Use inflateRawSync from node:zlib.

import { Zip } from '@buh/unzip';
import { readFileSync } from 'node:fs';

const file = readFileSync('archive.zip');

const zip = await Zip.create(file.buffer);
for (const entry of zip.iterator()) {
  console.log(entry.fileName, entry.lastModified(), entry.getData());
}

Browser / Custom

I pretend to do my own inflate implementation for study purpose, but I recommend use pako. Install npm install pako.

import { Zip } from '@buh/unzip';
import { inflateRaw } from 'pako';

document.addEventListener('DOMContentLoaded', () => {
  const input = document.querySelector('input[type=file]');
  if (!input) return;
  input.addEventListener('change', async () => {
    const file = input.files[0];
    const arrayBuffer = await file.arrayBuffer();

    const zip = await Zip.create(arrayBuffer, { inflate: inflateRaw });
    for (const entry of zip.iterator()) {
      console.log(entry.fileName, entry.lastModified(), entry.getData());
    }
  });
});

References

Chagelogs

You can track the changelogs in CHANGELOGS.md file.