/btar

A basic library for creating and reading USTar files in the browser.

Primary LanguageTypeScriptMIT LicenseMIT

btar

Description

A basic for library creating and reading USTar files in the browser.

Installing

Installing the latest commit

npm install fp-58/btar

Installing a specific version

npm install fp-58/btar#VERSION

Replace VERSION with the version tag.

Importing

ES6 Modules

import * as btar from "<uri-to-btar>/lib/esmodule.js";

CommonJS

const btar = require("btar");

AMD

define(["btar"], function (btar) {
    // Do something here
})

AMD (Simplified CommonJS Wrapping)

define(function (require) {
    const btar = require("btar");
})

HTML Script Tag

<script src="<uri-to-btar>/lib/index.js"></script>

The script declares a global variable named btar.

Usage

Reading an archive

const archive = await btar.TarArchive.fromBlob(file);

Extracting files from an archive

const FILE_TYPEFLAG = 0;

// Map of paths to files
let files = new Map();
for (const entry of archive) {
    const fullpath = entry.header.prefix + entry.header.name;
    if (entry.header.typeflag === FILE_TYPEFLAG) {
        files.set(fullpath, entry.content);
    }
}

Adding files to an archive

const file = new File(["Hello, world!"], "text.txt", {
    lastModified: Date.now()
});

archive.addFile("text.txt", file);