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:Creating Archives
UseBun.Archive.from() to create an archive from an object where keys are file paths and values are file contents:
- Strings - Text content
- Blobs - Binary data
- ArrayBufferViews (e.g.,
Uint8Array) - Raw bytes - ArrayBuffers - Raw binary data
Writing Archives to Disk
UseBun.Archive.write() to create and write an archive in one operation:
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:
/, 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):
!) 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):
File object includes:
name- The file path within the archive (always uses forward slashes/as separators)size- File size in byteslastModified- Modification timestamp- Standard
Blobmethods:text(),arrayBuffer(),stream(), etc.
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:- Corrupted/truncated archives -
Archive.from()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
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:
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:*- 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.
Compression
Bun.Archive supports gzip compression for both reading and writing:"gzip"- Enable gzip compressiontrue- Same as"gzip"falseorundefined- No compression
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.