nanotar Tar Utilities

0.3.0 · active · verified Sun Apr 19

nanotar is a compact and efficient utility library designed for creating and parsing Tar archives across various JavaScript environments, including Node.js, browsers, and edge runtimes. It prioritizes performance and a minimal footprint, making it suitable for resource-constrained applications. The current stable version is 0.3.0, with releases occurring as needed for enhancements and fixes, as demonstrated by recent updates to 0.2.0 and 0.3.0. A key differentiator is its universal JavaScript runtime compatibility and the inclusion of TypeScript types, ensuring robust development. It offers core functionalities for both generating `.tar` files and extracting their contents, supporting common Tar specifications. Unlike some alternatives, it aims to be dependency-free and focuses purely on the Tar format without additional compression (like gzip) built-in, allowing users to combine it with other compression libraries as needed.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create a Tar archive from an array of files and then parse its contents back into individual items, showing basic file and folder handling.

import { createTar, parseTar } from 'nanotar';

async function main() {
  const files = [
    { name: 'file1.txt', data: new TextEncoder().encode('Hello, nanotar!') },
    { name: 'folder/file2.json', data: new TextEncoder().encode('{"key": "value"}') }
  ];

  // Create a tar archive
  const tarBuffer = await createTar(files);
  console.log('Created tar archive with size:', tarBuffer.byteLength, 'bytes');

  // Simulate writing to disk and reading back (e.g., using Node.js fs)
  // For browser/edge, this would be a Blob or similar.
  // Example: fs.writeFileSync('archive.tar', Buffer.from(tarBuffer));
  // const readTarBuffer = new Uint8Array(fs.readFileSync('archive.tar'));

  // Parse the tar archive
  const parsedItems = [];
  for await (const item of parseTar(tarBuffer)) {
    parsedItems.push(item);
    console.log(`Parsed item: ${item.name} (type: ${item.type}, size: ${item.size})`);
    if (item.data) {
      console.log('  Content:', new TextDecoder().decode(item.data));
    }
  }

  console.log('Total items parsed:', parsedItems.length);
}

main().catch(console.error);

view raw JSON →