A ustar format implementation of Tar based off the FreeBSD 15.0 spec.
MIT License
Tar Stream is an implementation made from scratch following the FreeBSD 15.0 spec.
This implementation follows the ustar file format for creating and expanding Tarballs. While this format is compatible with most tar implementations, the format does have several limitations, including:
/
so each side does not exceed 155 and 100 characters respectively.Tar doesn't offer compression by default, so if you'd like to compress the Tarball, you may do so by piping it through a compression stream.
import { type TarFile, TarStream } from '@doctor/tar-stream'
await ReadableStream.from(readDir('./'))
.pipeThrough(
new TransformStream<string, TarFile>({
async transform(chunk, controller) {
if (chunk.endsWith('.ts')) {
controller.enqueue({
pathname: chunk,
size: (await Deno.stat(chunk)).size,
iterable: (await Deno.open(chunk)).readable,
})
}
},
}),
)
.pipeThrough(new TarStream())
.pipeThrough(new CompressionStream('gzip'))
.pipeTo((await Deno.create('./archive.tar.gz')).writable)
async function* readDir(path: string): AsyncGenerator<string> {
if (!path.endsWith('/')) {
path += '/'
}
for await (const dirEntry of Deno.readDir(path)) {
if (dirEntry.isFile) {
yield path + dirEntry.name
} else if (dirEntry.isDirectory) {
for await (const filePath of readDir(path + dirEntry.name)) {
yield filePath
}
}
}
}