std/archive/tar.ts

Deno standard library
Latest
import * as mod from "https://deno.land/std@0.157.0/archive/tar.ts";

Provides a Tar and Untar classes for compressing and decompressing arbitrary data.

Examples

Tar

import { Tar } from "https://deno.land/std@0.157.0/archive/tar.ts";
import { Buffer } from "https://deno.land/std@0.157.0/io/buffer.ts";
import { copy } from "https://deno.land/std@0.157.0/streams/conversion.ts";

const tar = new Tar();
const content = new TextEncoder().encode("Deno.land");
await tar.append("deno.txt", {
  reader: new Buffer(content),
  contentSize: content.byteLength,
});

// Or specifying a filePath.
await tar.append("land.txt", {
  filePath: "./land.txt",
});

// use tar.getReader() to read the contents.

const writer = await Deno.open("./out.tar", { write: true, create: true });
await copy(tar.getReader(), writer);
writer.close();

Untar

import { Untar } from "https://deno.land/std@0.157.0/archive/tar.ts";
import { ensureFile } from "https://deno.land/std@0.157.0/fs/ensure_file.ts";
import { ensureDir } from "https://deno.land/std@0.157.0/fs/ensure_dir.ts";
import { copy } from "https://deno.land/std@0.157.0/streams/conversion.ts";

const reader = await Deno.open("./out.tar", { read: true });
const untar = new Untar(reader);

for await (const entry of untar) {
  console.log(entry); // metadata

  if (entry.type === "directory") {
    await ensureDir(entry.fileName);
    continue;
  }

  await ensureFile(entry.fileName);
  const file = await Deno.open(entry.fileName, { write: true });
  // <entry> is a reader.
  await copy(entry, file);
}
reader.close();

Classes

A class to create a tar archive

A class to extract a tar archive