Skip to main content
On this page

@std/tar

Unstable

This @std package is experimental and its API may change without a major version bump.

Overview Jump to heading

Streaming utilities for working with tar archives.

Files are not compressed, only collected into the archive.

import { UntarStream } from "@std/tar/untar-stream";
import { dirname, normalize } from "@std/path";

for await (
  const entry of (await Deno.open("./out.tar.gz"))
    .readable
    .pipeThrough(new DecompressionStream("gzip"))
    .pipeThrough(new UntarStream())
) {
  const path = normalize(entry.path);
  await Deno.mkdir(dirname(path), { recursive: true });
  await entry.readable?.pipeTo((await Deno.create(path)).writable);
}

Add to your project Jump to heading

deno add jsr:@std/tar

See all symbols in @std/tar on

What is tar? Jump to heading

Tar (tape archive) is a widely-used file format for collecting multiple files into a single archive file, often for easier distribution or backup. It preserves file metadata like permissions and timestamps, making it suitable for system backups and software distribution.

Why use @std/tar? Jump to heading

Use this package to create or extract tar archives in a streaming fashion, which is efficient for large datasets and avoids high memory usage.

  • This module is streaming-first: use Web Streams to read/write entries without buffering whole archives.
  • Security: sanitize entry paths and guard against path traversal (".." segments) before writing to disk.
  • Tar is an archive format, not compression. Layer CompressionStream/DecompressionStream for gzip.
  • Preserve permissions/mtime if present in headers when extracting to maintain fidelity.

Examples Jump to heading

import { UntarStream } from "@std/tar/untar-stream";
import { dirname, isAbsolute, normalize } from "@std/path";

const outDir = "/safe/root";

for await (const entry of file.readable.pipeThrough(new UntarStream())) {
  const normalized = normalize(entry.path);
  // Prevent writing outside outDir
  if (normalized.includes("..") || isAbsolute(normalized)) continue;
  const dest = `${outDir}/${normalized}`;
  await Deno.mkdir(dirname(dest), { recursive: true });
  if (entry.readable) {
    await entry.readable.pipeTo((await Deno.create(dest)).writable);
  }
}

Did you find what you needed?

Privacy policy