GithubHelp home page GithubHelp logo

arborxr / libarchivejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nika-begiashvili/libarchivejs

0.0 1.0 0.0 3.55 MB

Archive library for browsers

License: MIT License

HTML 16.37% Shell 1.50% C 5.99% JavaScript 63.03% Python 8.67% Dockerfile 4.45%

libarchivejs's Introduction

Libarchivejs

npm version build status dependency status license

Overview

Libarchivejs is a archive tool for browser which can extract various types of compression, it's a port of libarchive to WebAssembly and javascript wrapper to make it easier to use. Since it runs on WebAssembly performance should be near native. Supported formats: ZIP, 7-Zip, RAR v4, RAR v5, TAR. Supported compression: GZIP, DEFLATE, BZIP2, LZMA

How to use

Install with npm i libarchive.js and use it as a ES module.

The library consists of two parts: ES module and webworker bundle, ES module part is your interface to talk to library, use it like any other module. The webworker bundle lives in the libarchive.js/dist folder so you need to make sure that it is available in your public folder since it will not get bundled if you're using bundler (it's all bundled up already) and specify correct path to Archive.init() method

import {Archive} from 'libarchive.js/main.js';

Archive.init({
    workerUrl: 'libarchive.js/dist/worker-bundle.js'
});

document.getElementById('file').addEventListener('change', async (e) => {
    const file = e.currentTarget.files[0];

    const archive = await Archive.open(file);
    let obj = await archive.extractFiles();
    
    console.log(obj);
});

// outputs
{
    ".gitignore": {File},
    "addon": {
        "addon.py": {File},
        "addon.xml": {File}
    },
    "README.md": {File}
}

More options

To get file listing without actually decompressing archive, use one of these methods

    await archive.getFilesObject();
    // outputs
    {
        ".gitignore": {CompressedFile},
        "addon": {
            "addon.py": {CompressedFile},
            "addon.xml": {CompressedFile}
        },
        "README.md": {CompressedFile}
    }

    await archive.getFilesArray();
    // outputs
    [
        {file: {CompressedFile}, path: ""},
        {file: {CompressedFile},   path: "addon/"},
        {file: {CompressedFile},  path: "addon/"},
        {file: {CompressedFile},  path: ""}
    ]

If these methods get called after archive.extractFiles(); they will contain actual files as well.

Decompression might take a while for larger files. To track each file as it gets extracted, archive.extractFiles accepts callback

    archive.extractFiles((entry) => { // { file: {File}, path: {String} }
        console.log(entry);
    });

Extract single file from archive

To extract a single file from the archive you can use the extract() method on the returned CompressedFile.

    const filesObj = await archive.getFilesObject();
    const file = await filesObj['.gitignore'].extract();

Check for encrypted data

    const archive = await Archive.open(file);
    await archive.hasEncryptedData();
    // true - yes
    // false - no
    // null - can not be determined

Extract encrypted archive

    const archive = await Archive.open(file);
    await archive.usePassword("password");
    let obj = await archive.extractFiles();

How it works

Libarchivejs is a port of the popular libarchive C library to WASM. Since WASM runs in the current thread, the library uses WebWorkers for heavy lifting. The ES Module (Archive class) is just a client for WebWorker. It's tiny and doesn't take up much space.

Only when you actually open archive file will the web worker be spawned and WASM module will be downloaded. Each Archive.open call corresponds to each WebWorker.

After calling an extractFiles worker, it will be terminated to free up memory. The client will still work with cached data.

libarchivejs's People

Contributors

nika-begiashvili avatar dependabot[bot] avatar alexandrebonaventure avatar btzr-io avatar tranquilmarmot avatar dbolton avatar

Watchers

James Cloos avatar

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.