tar
The archive/tar module creates and extracts TAR archives in JSH.
It supports simple in-memory helpers, stream-style APIs, and a file-based Tar class.
Use the API that matches your task:
- Use
tarSync()anduntarSync()when the archive is already in memory. - Use
Tarwhen you want to read files, save.tarfiles, or extract them to disk. - Use
createTar()andcreateUntar()when you want event-driven processing.
Installation
const tar = require('archive/tar');tarSync()
Creates a TAR archive synchronously.
Syntax
tarSync(data)Parameters
dataString | ArrayBuffer | Uint8Array | Number[] | Object[]
If data is a single string or byte buffer, the created entry name defaults to data.
If data is an array, each item should be an entry object such as { name, data }.
Return value
Returns an ArrayBuffer that contains TAR archive bytes.
This form is useful for quick tests or for building an archive before writing it somewhere else.
Usage example
| |
| |
untarSync()
Extracts TAR archive bytes synchronously and returns entry objects.
Syntax
untarSync(buffer)Parameters
bufferArrayBuffer | Uint8Array | Number[]
Return value
Returns an array of entry objects.
Each entry can include name, data, mode, size, isDir, modified, typeflag, type,
and linkname.
Usage example
| |
tar()
Provides a callback-style asynchronous wrapper for TAR archive creation.
Syntax
tar(data, callback)The callback follows the (err, archive) form.
untar()
Provides a callback-style asynchronous wrapper for TAR extraction.
Syntax
untar(buffer, callback)The callback follows the (err, entries) form.
Use these wrappers when you want callback-based control flow without managing stream events directly.
Usage example
| |
createTar()
Creates a stream-style TAR writer.
The returned object accepts entry objects through write() and emits archive bytes through the
data event when end() is called.
This API is event-driven, but it still finalizes work on end() rather than emitting extracted
files progressively like a Node.js file stream.
Syntax
createTar()createUntar()
Creates a stream-style TAR reader.
Write archive bytes with write(), then call end() to emit one entry event per extracted item.
Syntax
createUntar()Usage example
| |
Tar
Tar is a file-oriented helper class for building, saving, loading, and extracting TAR archives.
Constructor
new tar.Tar(filePath?)If filePath is provided, the archive is loaded from that file.
addFile()
Reads a file from the filesystem and appends it as an archive entry.
addFile(filePath[, entryName])addBuffer()
Appends a string or byte buffer as an archive entry.
addBuffer(data, entryName[, options])addEntry()
Appends an archive entry object directly.
addEntry(entry)Supported TAR entry fields:
nameStringrequired entry pathdataString | ArrayBuffer | Uint8Array | Number[]file contentmodeNumberfile modemodifiedDatemodification timetypeStringsuch asfile,dir,symlink, orlinktypeflagNumberraw TAR type flaglinknameStringlink target forsymlinkorlinkisDirBooleanmarks a directory entry
addFile() is the most direct option when the source data already exists on disk.
addBuffer() is useful when the content is generated in memory.
addEntry() is the lowest-level form and is required for TAR directory or link metadata.
getEntries()
Returns a shallow copy of the current archive entries.
getEntries()writeTo()
Writes the archive to a file.
writeTo(filePath)extractAllTo()
Extracts entries to a directory.
extractAllTo(outputDir[, overwrite])
extractAllTo(outputDir, options)
extractAllTo(outputDir, overwrite, options)options supports:
overwriteBooleanoverwrite existing files whentruefilterFunction | RegExp | String | String[]selects which entries to extract
Usage example
| |
Usage example: create from files and extract again
| |
Usage example: filter extraction
| |
Notes
- Directory entries are returned with
isDir: true, and their names end with/. filtermay be a callback,RegExp, string match, or array of entry names.extractAllTo()throws an error if the destination file already exists andoverwriteisfalse.- TAR-specific link metadata is supported through
type: 'symlink'ortype: 'link'withlinkname.