Skip to content

ofk/libarchive-wasm

Repository files navigation

libarchive-wasm

Build Status npm version npm downloads MIT License

libarchive-wasm is a JavaScript library for reading various archive and compression formats. It's port of libarchive to WebAssembly and JavaScript wrapper to make it easier to use, since it runs performance should be near native.

This project was inspired by libarchive.js. libarchive-wasm only has a low level API for simpler Int8Array input and output. You can implement for different file objects and WebWorkers etc. in the browser and NodeJS as needed.

Feature

  • Supported formats: ZIP, 7-Zip, RAR v4, RAR v5, TAR
  • Supported compression: GZIP, DEFLATE, BZIP2, LZMA
  • Built with emscripten with support for WebAssembly.instantiateStreaming (Support NodeJS v18+)

Usage

npm i libarchive-wasm

Node

import { readFile } from 'node:fs/promises';
import { ArchiveReader, libarchiveWasm } from 'libarchive-wasm';

(async () => {
  const data = await readFile('example.zip');
  const mod = await libarchiveWasm();
  const reader = new ArchiveReader(mod, new Int8Array(data));
  for (const entry of reader.entries()) {
    const result = {
      pathname: entry.getPathname(),
      size: entry.getSize(),
    };
    if (result.pathname.endsWith('.md')) {
      result.data = new TextDecoder().decode(entry.readData());
    }
    console.log(result);
  }
  reader.free();
})();

Browser

import { ArchiveReader, libarchiveWasm } from 'libarchive-wasm';

document.getElementById('upload').addEventListener('change', async (e) => {
  const file = e.currentTarget.files[0];
  const data = await file.arrayBuffer();
  const mod = await libarchiveWasm();
  const reader = new ArchiveReader(mod, new Int8Array(data));
  for (const entry of reader.entries()) {
    const result = {
      pathname: entry.getPathname(),
      size: entry.getSize(),
    };
    if (result.pathname.endsWith('.md')) {
      result.data = new TextDecoder().decode(entry.readData());
    }
    console.log(result);
  }
  reader.free();
});

Node Worker by Minlink

Browser Worker by Minlink