Archive
public final class Archive : Sequence
A sequence of uncompressed or compressed ZIP entries.
You use an Archive to create, read or update ZIP files.
To read an existing ZIP file, you have to pass in an existing file URL and AccessMode.read:
var archiveURL = URL(fileURLWithPath: "/path/file.zip")
var archive = Archive(url: archiveURL, accessMode: .read)
An Archive is a sequence of entries. You can
iterate over an archive using a for-in loop to get access to individual Entry objects:
for entry in archive {
print(entry.path)
}
Each Entry in an Archive is represented by its path. You can
use path to retrieve the corresponding Entry from an Archive via subscripting:
let entry = archive['/path/file.txt']
To create a new Archive, pass in a non-existing file URL and AccessMode.create. To modify an
existing Archive use AccessMode.update:
var archiveURL = URL(fileURLWithPath: "/path/file.zip")
var archive = Archive(url: archiveURL, accessMode: .update)
try archive?.addEntry("test.txt", relativeTo: baseURL, compressionMethod: .deflate)
-
An error that occurs during reading, creating or updating a ZIP file.
See moreDeclaration
Swift
public enum ArchiveError : Error -
Declaration
Swift
public enum AccessMode : UInt -
URL of an Archive’s backing file.
Declaration
Swift
public let url: URL -
Access mode for an archive file.
Declaration
Swift
public let accessMode: AccessMode -
Initializes a new ZIP
Archive.You can use this initalizer to create new archive files or to read and update existing ones.
To read existing ZIP files, pass in an existing file URL and
AccessMode.read.To create a new ZIP file, pass in a non-existing file URL and
AccessMode.create.To update an existing ZIP file, pass in an existing file URL and
AccessMode.update.Declaration
Swift
public init?(url: URL, accessMode mode: AccessMode)Parameters
urlFile URL to the receivers backing file.
modeAccess mode of the receiver.
Return Value
An archive initialized with a backing file at the passed in file URL and the given access mode or
nilif the following criteria are not met:- The file URL must point to an existing file for
AccessMode.read - The file URL must point to a non-existing file for
AccessMode.write - The file URL must point to an existing file for
AccessMode.update
- The file URL must point to an existing file for
-
Declaration
Swift
public func makeIterator() -> AnyIterator<Entry> -
Retrieve the ZIP
Entrywith the givenpathfrom the receiver.Note
The ZIP file format specification does not enforce unique paths for entries. Therefore an archive can contain multiple entries with the same path. This method always returns the first
Entrywith the givenpath.Declaration
Swift
public subscript(path: String) -> Entry? { get }Parameters
pathA relative file path identifiying the corresponding
Entry.Return Value
An
Entrywith the givenpath. Otherwise,nil.
-
Write files, directories or symlinks to the receiver.
Throws
An error if the source file cannot be read or the receiver is not writable.Declaration
Swift
public func addEntry(with path: String, relativeTo baseURL: URL, compressionMethod: CompressionMethod = .none, bufferSize: UInt32 = defaultWriteChunkSize, progress: Progress? = nil) throwsParameters
pathThe path that is used to identify an
Entrywithin theArchivefile.baseURLThe base URL of the
Entryto add. ThebaseURLcombined withpathmust form a fully qualified file URL.compressionMethodIndicates the
CompressionMethodthat should be applied toEntry.bufferSizeThe maximum size of the write buffer and the compression buffer (if needed).
progressA progress object that can be used to track or cancel the add operation.
-
addEntry(with:type:uncompressedSize:modificationDate:permissions:compressionMethod:bufferSize:progress:provider:)Write files, directories or symlinks to the receiver.
Throws
An error if the source data is invalid or the receiver is not writable.Declaration
Swift
public func addEntry(with path: String, type: Entry.EntryType, uncompressedSize: UInt32, modificationDate: Date = Date(), permissions: UInt16? = nil, compressionMethod: CompressionMethod = .none, bufferSize: UInt32 = defaultWriteChunkSize, progress: Progress? = nil, provider: Provider) throwsParameters
pathThe path that is used to identify an
Entrywithin theArchivefile.typeIndicates the
Entry.EntryTypeof the added content.uncompressedSizeThe uncompressed size of the data that is going to be added with
provider.modificationDateA
Datedescribing the file modification date of theEntry. Default is the currentDate.permissionsPOSIX file permissions for the
Entry. Default is0o644for files and symlinks and0o755for directories.compressionMethodIndicates the
CompressionMethodthat should be applied toEntry.bufferSizeThe maximum size of the write buffer and the compression buffer (if needed).
progressA progress object that can be used to track or cancel the add operation.
providerA closure that accepts a position and a chunk size. Returns a
Datachunk. -
Remove a ZIP
Entryfrom the receiver.Throws
An error if theEntryis malformed or the receiver is not writable.Declaration
Swift
public func remove(_ entry: Entry, bufferSize: UInt32 = defaultReadChunkSize, progress: Progress? = nil) throwsParameters
entryThe
Entryto remove.bufferSizeThe maximum size for the read and write buffers used during removal.
progressA progress object that can be used to track or cancel the remove operation.
-
The number of the work units that have to be performed when removing
entryfrom the receiver.Declaration
Swift
public func totalUnitCountForRemoving(_ entry: Entry) -> Int64Parameters
entryThe entry that will be removed.
Return Value
The number of the work units.
-
The number of the work units that have to be performed when reading
entryfrom the receiver.Declaration
Swift
public func totalUnitCountForReading(_ entry: Entry) -> Int64Parameters
entryThe entry that will be read.
Return Value
The number of the work units.
-
The number of the work units that have to be performed when adding the file at
urlto the receiver.Declaration
Swift
public func totalUnitCountForAddingItem(at url: URL) -> Int64Parameters
entryThe entry that will be removed.
Return Value
The number of the work units.
View on GitHub
Archive Class Reference