Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/archive/untar.ts>Untar

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
class Untar
import { Untar } from "https://deno.land/std@0.209.0/archive/untar.ts";

Overview

A class to extract from a tar archive. Tar archives allow for storing multiple files in a single file (called an archive, or sometimes a tarball). These archives typically have the '.tar' extension.

Supported file formats

Only the ustar file format is supported. This is the most common format. The pax file format may also be read, but additional features, such as longer filenames may be ignored.

Usage

The workflow is to create a Untar instance referencing the source of the tar file. You can then use the untar reference to extract files one at a time. See the worked example below for details.

Understanding compression

A tar archive may be compressed, often identified by the .tar.gz extension. This utility does not support decompression which must be done before extracting the files.

Examples

Example 1

import { Untar } from "https://deno.land/std@0.209.0/archive/untar.ts";
import { ensureFile } from "https://deno.land/std@0.209.0/fs/ensure_file.ts";
import { ensureDir } from "https://deno.land/std@0.209.0/fs/ensure_dir.ts";
import { copy } from "https://deno.land/std@0.209.0/streams/copy.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();

Constructors

new
Untar(reader: Reader)

Properties

block: Uint8Array
reader: Reader

Methods

extract(): Promise<TarEntry | null>

Extract the next entry of the tar archive.

[Symbol.asyncIterator](): AsyncIterableIterator<TarEntry>

Iterate over all entries of the tar archive.