Swift wrapper for libzip — library for reading, creating, and modifying zip archives.
SwiftZip is a Swift wrapper for libzip providing an API to read, create and modify zip archives.
Files can be added from data buffers, files, or compressed data copied directly from other zip archives.
Changes made without closing the archive can be reverted.
Note: SwiftZip is currently under development and API may change slightly as the project evolves.
Opening and inspecting an archive:
do {
// Open an archive for reading
let archive = try ZipArchive(url: archiveUrl)
// Enumerate entries in the archive
for entry in archive.entries {
// Get basic entry information
let name = try entry.getName()
let size = try entry.stat().size
print("\(name) -> \(size as Any)")
// Read entry contents into a `Data` instance
let data = try entry.data()
print(data)
}
} catch {
// Handle possible errors
print("\(error)")
}
Creating an archive:
do {
// Open an archive for writing, overwriting any existing file
let archive = try ZipMutableArchive(url: archiveUrl, flags: [.create, .truncate])
// Load the test data
let data = try Data(contentsOf: dataUrl)
// Create a data source and add it to the archive
let source = try ZipSource(data: data)
try archive.addFile(name: "filename.dat", source: source)
// Commit changes and close the archive
// Alternatively call `discard` to rollback any changes
try archive.close()
} catch {
// Handle possible errors
print("\(error)")
}
Auto-generated documentation based on libzip manual is available at https://swiftzip.github.io/.
SwiftZip is designed to be a thin wrapper aroung libzip. Please refer to the original libzip documentation to get
more details on the underlying implementation: https://libzip.org/documentation/.
Current libzip API mapping and coverage is available at API.md
To depend on the SwiftZip package, you need to declare your dependency in your Package.swift
file:
dependencies: [
.package(url: "https://github.com/SwiftZip/SwiftZip.git", .branch("master")),
// ...
]
and add “SwiftZip” to your application/library target dependencies, e.g. like this:
.target(name: "BestExampleApp", dependencies: [
"SwiftZip",
// ...
])
SwiftZip requires BZip2
and OpenSSL
development packages to be installed when building on Linux.
You can install the required dependencies using apt
on Ubuntu:
apt-get install libbz2-dev
apt-get install libssl-dev
SwiftZip in currently under development. Please open an issue or submit a pull request in case you find any
issues or have any improvement ideas.
Sources/zip
directory and exposed as zip
packageSources/zip/libzip
is a submodule referencing relevant libzip source codeSources/zip/libzip-patches
folder contains patches to be applied to the libzip header files, so they are compatible with the Swift package managerSources/zip/include
contains public headers for the libzip as required by the Swift package managerSources/zip/include-private
contains patched private headers to build libzipSources/SwiftZip
directory and exposed as SwiftZip
packageThe SwiftZip wrapper is designed to make libzip updates as easy as possible.
To update the underlying library, use the ./Tools/libzip-update.sh
script to pull the latest version in
the Sources/zip/libzip
submodule and update public headers.