I wrote a TAR reader/writer in Swift.
swift-tar is a pure Swift library for reading, writing, and extracting TAR archives.
- Foundation-free - works without Foundation or any system framework
- Cross-platform - macOS, Linux, Windows, WebAssembly, Embedded Swift
- GNU & PAX extensions - transparent handling of long paths, long link names, and PAX extended headers
If you already have the whole archive in memory, the basic API is small:
import Tar
let archive = Archive(data: archiveBytes)
for entry in archive {
let path = entry.fields.path()
let size = entry.fields.size
print("\(path) (\(size) bytes)")
}
For larger inputs, there is also a streaming reader and streaming extractor:
import Tar
var reader = TarReader()
var extractor = try TarExtractor().streamingExtractor(to: "output")
for chunk in chunks {
try extractor.consume(reader.append(chunk))
}
try extractor.consume(reader.finish())
let result = try extractor.finish()
print("Extracted \(result.extractedEntries) entries")
In one local benchmark, extracting the 3.5 GB swift-6.3-RELEASE-ubuntu24.04.tar archive on macOS was slightly faster than bsdtar in this setup.
The repo is here: https://github.com/kateinoigakukun/swift-tar
MIT licensed.
Feedback and any use case ideas are welcome.