A Swift Package that compiles libarchive from source and provides an idiomatic Swift API for reading and writing archive files.
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/marcprux/swift-archive", from: "3.8.6")
]Depend on
marcprux/swift-archive, notlibarchive/libarchive— only this fork's release tags carry thePackage.swiftmanifest that SwiftPM needs. See Synchronizing Releases with Upstream for how those tags are produced.
Then add "Archive" to your target's dependencies.
import Archive
// From a file
let reader = try ArchiveReader(path: "/path/to/archive.tar.gz")
try reader.forEachEntry { entry, reader in
print(entry.pathname)
let data = try reader.readData()
}
// From in-memory data
let reader = try ArchiveReader(data: archiveData)
let entries = try reader.listEntries()import Archive
// In-memory tar.gz
let writer = try ArchiveWriter(format: .tar, filters: [.gzip])
try writer.writeEntry(
ArchiveEntry(pathname: "hello.txt", size: Int64(data.count)),
data: data
)
let archiveData = try writer.finish()
// Write to file
let writer = try ArchiveWriter(path: "/tmp/out.zip", format: .zip)
try writer.writeEntry(
ArchiveEntry(pathname: "file.txt", size: Int64(data.count)),
data: data
)
try writer.close()let reader = try ArchiveReader(data: archiveData)
try reader.extractAll(to: "/tmp/output", options: .default)// Compress data into an archive
let compressed = try myData.compress(as: "file.txt", format: .tar, filters: [.gzip])
// Decompress archive data
let files = try compressed.decompressArchive() // [String: Data]
// List entries
let entries = try archiveData.archiveEntries()| Format | Read | Write |
|---|---|---|
| tar (pax, gnutar, ustar, v7) | Yes | Yes |
| zip | Yes | Yes |
| 7-Zip | Yes | Yes |
| cpio | Yes | Yes |
| ar | Yes | Yes |
| xar | Yes | Yes |
| ISO 9660 | Yes | Yes |
| shar | No | Yes |
| mtree | Yes | Yes |
| WARC | Yes | Yes |
| RAW | Yes | Yes |
| RAR, LHA, CAB | Yes | No |
| Filter | Read | Write | Trait |
|---|---|---|---|
| gzip | Yes | Yes | GzipSupport (default) |
| bzip2 | Yes | Yes | Bzip2Support (default) |
| compress | Yes | Yes | Always available |
| xz/lzma | Yes | Yes | LZMASupport (requires liblzma) |
| zstd | Yes | Yes | ZstdSupport (requires libzstd) |
| lz4 | Yes | Yes | Always available |
Combine format and filters for common archive types:
// .tar.gz
try ArchiveWriter(format: .tar, filters: [.gzip])
// .tar.bz2
try ArchiveWriter(format: .tar, filters: [.bzip2])
// .tar.xz (requires LZMASupport trait)
try ArchiveWriter(format: .tar, filters: [.xz])The package uses Swift Package Manager traits to control optional compression library support:
| Trait | Default | Description |
|---|---|---|
GzipSupport |
Enabled | zlib (available in macOS SDK) |
Bzip2Support |
Enabled | bzip2 (available in macOS SDK) |
LZMASupport |
Disabled | Requires liblzma (e.g., brew install xz) |
ZstdSupport |
Disabled | Requires libzstd (e.g., brew install zstd) |
Enable additional traits:
swift build --traits LZMASupport,ZstdSupportAll operations throw ArchiveError on failure:
do {
let reader = try ArchiveReader(data: corruptData)
_ = try reader.listEntries()
} catch let error as ArchiveError {
print(error.code) // libarchive errno
print(error.message) // Human-readable description
}Control extraction behavior with ExtractOptions:
let reader = try ArchiveReader(data: archiveData)
try reader.extractAll(to: "/tmp/out", options: [.permissions, .time, .secure])Available options: .permissions, .time, .owner, .acl, .xattr, .fflags, .noOverwrite, .noOverwriteNewer, .secure.
ArchiveEntry,ArchiveError,ArchiveFormat,ArchiveFilter,FileType, andExtractOptionsare allSendableArchiveReaderandArchiveWriterare reference types (classes) that should not be shared across threads
The package has three targets:
- CArchive — C target that compiles libarchive sources directly
- Archive — Swift wrapper with idiomatic API
- ArchiveTests — Tests using Swift Testing framework
This fork tracks libarchive/libarchive and republishes each upstream release under the same version number, with the Swift overlay added. The model has three moving parts:
- The
swiftbranch is the integration branch: the upstream tree plus the additive Swift overlay (Package.swift,Sources/,Tests/, theCliblzma/Clibzstdmodule maps,libarchive/config_spm.h,.spi.yml,swift-ci.yml, and thisREADME-SWIFT.md). Because the overlay only adds files and never edits upstream sources, merging upstream is conflict-free. - A release tag
vX.Y.Zreuses upstream's exact version number, but points at a commit on theswiftbranch — i.e. one that containsPackage.swift. That is what makes it resolvable by SwiftPM frommarcprux/swift-archive. - This repository also mirrors upstream's own
vX.Y.Ztags, which point at pure-upstream "Release X.Y.Z" commits that have noPackage.swift. The fork's release tag therefore has to override the same-named upstream tag — that is what the force-push in step 2 does.
⚠️ The invariant that must always hold: a release tag must point at a commit that containsPackage.swift. If avX.Y.Ztag is left pointing at the upstream "Release X.Y.Z" commit,swift package resolveagainst this repo fails because that commit has no manifest.
The whole flow is scripted. To mirror a specific upstream release:
Scripts/swift-release.sh 3.8.7To release whatever the most recent upstream release tag is, run it with no argument:
Scripts/swift-release.shUseful flags: --dry-run (print the plan and resolved version without changing anything),
--no-test, --no-release (tag only), -y/--yes (skip the confirmation prompt), --help.
The script performs exactly the manual steps below, and refuses to publish a tag whose commit
does not contain Package.swift.
The manual steps are documented here as the reference the script implements.
git remote add upstream https://github.com/libarchive/libarchive.gitgit fetch upstream --tags
git checkout swift
git merge --no-edit upstream/master # additive overlay → no conflicts
swift build && swift test # confirm the Swift package still builds
git push origin swiftPick the upstream version to mirror (e.g. 3.8.7). Upstream cuts patch releases from its
patch/3.x branches rather than master, so merge the tag (not just upstream/master) to
be sure the release-specific fixes are included:
VERSION=3.8.7
git fetch upstream --tags
git checkout swift
git merge --no-edit "v$VERSION" # additive overlay → no conflicts
swift build && swift test
git push origin swift
# Tag the overlay commit with the upstream version, overriding the mirrored upstream tag:
git tag -f -a "v$VERSION" -m "libarchive $VERSION + Swift overlay"
git push origin -f "v$VERSION" # force: replaces the upstream-mirrored tag of the same nameConfirm the tag is SwiftPM-consumable before announcing it:
git ls-tree --name-only "v$VERSION" | grep -qx 'Package.swift' \
&& echo "OK: v$VERSION includes Package.swift" \
|| echo "ERROR: v$VERSION has no Package.swift — do not release"gh release create "v$VERSION" \
--repo marcprux/swift-archive \
--title "v$VERSION" \
--notes "Based on libarchive $VERSION with the Swift Package overlay."If a tag/release already points at a pure-upstream commit (no Package.swift — as v3.8.5 and
v3.8.7 once did), delete it and re-cut it onto the overlay:
VERSION=3.8.7
gh release delete "v$VERSION" --repo marcprux/swift-archive --yes --cleanup-tag
# then repeat steps 2–3 above-
git ls-tree v$VERSIONlistsPackage.swift -
swift buildandswift testpass at the tagged commit - A throwaway package depending on
marcprux/swift-archivewithfrom: "$VERSION"resolves to the new tag (swift package resolve) - The GitHub release for
v$VERSIONis marked Latest
This repository is a purely additive fork of libarchive/libarchive. No upstream files are modified, deleted, or patched; the fork only overlays the following Swift-specific additions on top of the original tree:
Package.swift— Swift Package Manager manifest that compiles libarchive sources directly and declares the Swift targets and traits.Sources/Archive/— The idiomatic Swift wrapper API (ArchiveReader,ArchiveWriter,ArchiveEntry, etc.).Sources/Cliblzma/andSources/Clibzstd/— Module maps for optional system libraries used by theLZMASupportandZstdSupporttraits.Tests/ArchiveTests/— Swift Testing-based test suite for the wrapper.Scripts/swift-release.sh— Automates merging an upstream release tag and publishing the version-matched Swift release (see Synchronizing Releases with Upstream).README-SWIFT.md— This document.
Because the fork is strictly additive, it can be kept in sync with upstream by merging libarchive/libarchive directly with no conflict resolution against the C sources. The Swift overlay treats libarchive as an upstream dependency consumed in-place via SwiftPM, rather than vendoring a snapshot. See Synchronizing Releases with Upstream for the exact workflow used to merge upstream and publish version-matched releases.