Skip to main content
Bun provides a fast, native implementation for working with tar archives through Bun.Archive. It supports creating archives from in-memory data, extracting archives to disk, and reading archive contents without extraction.

Quickstart

Create an archive from files:
Extract an archive:
Read archive contents without extracting:

Creating Archives

Use new Bun.Archive() to create an archive from an object where keys are file paths and values are file contents. By default, archives are uncompressed:
File contents can be:
  • Strings - Text content
  • Blobs - Binary data
  • ArrayBufferViews (e.g., Uint8Array) - Raw bytes
  • ArrayBuffers - Raw binary data

Writing Archives to Disk

Use Bun.write() to write an archive to disk:

Getting Archive Bytes

Get the archive data as bytes or a Blob:

Extracting Archives

From Existing Archive Data

Create an archive from existing tar/tar.gz data:

Extracting to Disk

Use .extract() to write all files to a directory:
The target directory is created automatically if it doesn’t exist. Existing files are overwritten. The returned count includes files, directories, and symlinks (on POSIX systems). Note: On Windows, symbolic links in archives are always skipped during extraction. Bun does not attempt to create them regardless of privilege level. On Linux and macOS, symlinks are extracted normally. Security note: Bun.Archive validates paths during extraction, rejecting absolute paths (POSIX /, Windows drive letters like C:\ or C:/, and UNC paths like \\server\share). Path traversal components (..) are normalized away (e.g., dir/sub/../file becomes dir/file) to prevent directory escape attacks.

Filtering Extracted Files

Use glob patterns to extract only specific files. Patterns are matched against archive entry paths normalized to use forward slashes (/). Positive patterns specify what to include, and negative patterns (prefixed with !) specify what to exclude. Negative patterns are applied after positive patterns, so using only negative patterns will match nothing (you must include a positive pattern like ** first):
Use negative patterns (prefixed with !) to exclude files. When mixing positive and negative patterns, entries must match at least one positive pattern and not match any negative pattern:

Reading Archive Contents

Get All Files

Use .files() to get archive contents as a Map of File objects without extracting to disk. Unlike extract() which processes all entry types, files() returns only regular files (no directories):
Each File object includes:
  • name - The file path within the archive (always uses forward slashes / as separators)
  • size - File size in bytes
  • lastModified - Modification timestamp
  • Standard Blob methods: text(), arrayBuffer(), stream(), etc.
Note: files() loads file contents into memory. For large archives, consider using extract() to write directly to disk instead.

Error Handling

Archive operations can fail due to corrupted data, I/O errors, or invalid paths. Use try/catch to handle these cases:
Common error scenarios:
  • Corrupted/truncated archives - new Archive() loads the archive data; errors may be deferred until read/extract operations
  • Permission denied - extract() throws if the target directory is not writable
  • Disk full - extract() throws if there’s insufficient space
  • Invalid paths - Operations throw for malformed file paths
The count returned by extract() includes all successfully written entries (files, directories, and symlinks on POSIX systems). Security note: Bun.Archive automatically validates paths during extraction. Absolute paths (POSIX /, Windows drive letters, UNC paths) and unsafe symlink targets are rejected. Path traversal components (..) are normalized away to prevent directory escape. For additional security with untrusted archives, you can enumerate and validate paths before extraction:
When using files() with a glob pattern, an empty Map is returned if no files match:

Filtering with Glob Patterns

Pass a glob pattern to filter which files are returned:
Supported glob patterns (subset of Bun.Glob syntax):
  • * - Match any characters except /
  • ** - Match any characters including /
  • ? - Match single character
  • [abc] - Match character set
  • {a,b} - Match alternatives
  • !pattern - Exclude files matching pattern (negation). Must be combined with positive patterns; using only negative patterns matches nothing.
See Bun.Glob for the full glob syntax including escaping and advanced patterns.

Compression

Bun.Archive creates uncompressed tar archives by default. Use { compress: "gzip" } to enable gzip compression:
The options accept:
  • No options or undefined - Uncompressed tar (default)
  • { compress: "gzip" } - Enable gzip compression at level 6
  • { compress: "gzip", level: number } - Gzip with custom level 1-12 (1 = fastest, 12 = smallest)

Examples

Bundle Project Files

Extract and Process npm Package

Create Archive from Directory

Reference

Note: The following type signatures are simplified for documentation purposes. See packages/bun-types/bun.d.ts for the full type definitions.