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

std/archive/tar.ts>Tar

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

Overview

A class to create 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.

Usage

The workflow is to create a Tar instance, append files to it, and then write the tar archive to the filesystem (or other output stream). See the worked example below for details.

Compression

Tar archives are not compressed by default. If you want to compress the archive, you may compress the tar archive after creation, but this capability is not provided here.

File format and limitations

The ustar file format is used for creating the archive file. While this format is compatible with most tar readers, the format has several limitations, including:

  • Files must be smaller than 8GiB
  • Filenames (including path) must be shorter than 256 characters
  • Filenames (including path) cannot contain non-ASCII characters
  • Sparse files are not supported

Examples

Example 1

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

const tar = new Tar();

// Now that we've created our tar, let's add some files to it:

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

// This file is sourced from the filesystem (and renamed in the archive)
await tar.append("filename_in_archive.txt", {
  filePath: "./filename_on_filesystem.txt",
});

// Now let's write the tar (with it's two files) to the filesystem
// 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();

Constructors

new
Tar()

Methods

append(filenameInArchive: string, source: TarOptions)

Append a file or reader of arbitrary content to this tar archive. Directories appended to the archive append only the directory itself to the archive, not its contents. To add a directory and its contents, recursively append the directory's contents. Directories and subdirectories will be created automatically in the archive as required.

Get a Reader instance for this tar archive.