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 -
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. The
modeparameter indicates the intended usage of the archive:.read,.createor.update.Note
- The file URL must point to an existing file for
AccessMode.read. - The file URL must point to a non-existing file for
AccessMode.create. - The file URL must point to an existing file for
AccessMode.update.
Declaration
Swift
public init?(url: URL, accessMode mode: AccessMode, preferredEncoding: String.Encoding? = nil)Parameters
urlFile URL to the receivers backing file.
modeAccess mode of the receiver.
preferredEncodingEncoding for entry paths. Overrides the encoding specified in the archive. This encoding is only used when decoding paths from the receiver. Paths of entries added with
addEntryare always UTF-8 encoded.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
-
Initializes a new in-memory ZIP
Archive.You can use this initalizer to create new in-memory archive files or to read and update existing ones.
Note
- The backing
datamust contain a valid ZIP archive forAccessMode.readandAccessMode.update. - The backing
datamust be empty (or omitted) forAccessMode.create.
Declaration
Swift
public init?(data: Data = Data(), accessMode mode: AccessMode, preferredEncoding: String.Encoding? = nil)Parameters
dataDataobject used as backing for in-memory archives.modeAccess mode of the receiver.
preferredEncodingEncoding for entry paths. Overrides the encoding specified in the archive. This encoding is only used when decoding paths from the receiver. Paths of entries added with
addEntryare always UTF-8 encoded.Return Value
An in-memory archive initialized with passed in backing data.
- The backing
-
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 identifying the corresponding
Entry.Return Value
An
Entrywith the givenpath. Otherwise,nil. -
Returns a
Dataobject containing a representation of the receiver.Declaration
Swift
public var data: Data? { get } -
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.
-
Read a ZIP
Entryfrom the receiver and write it tourl.Throws
An error if the destination file cannot be written or the entry contains malformed content.Declaration
Swift
public func extract(_ entry: Entry, to url: URL, bufferSize: Int = defaultReadChunkSize, skipCRC32: Bool = false, progress: Progress? = nil) throws -> CRC32Parameters
entryThe ZIP
Entryto read.urlThe destination file URL.
bufferSizeThe maximum size of the read buffer and the decompression buffer (if needed).
skipCRC32Optional flag to skip calculation of the CRC32 checksum to improve performance.
progressA progress object that can be used to track or cancel the extract operation.
Return Value
The checksum of the processed content or 0 if the
skipCRC32flag was set totrue. -
Read a ZIP
Entryfrom the receiver and forward its contents to aConsumerclosure.Throws
An error if the destination file cannot be written or the entry contains malformed content.Declaration
Swift
public func extract(_ entry: Entry, bufferSize: Int = defaultReadChunkSize, skipCRC32: Bool = false, progress: Progress? = nil, consumer: Consumer) throws -> CRC32Parameters
entryThe ZIP
Entryto read.bufferSizeThe maximum size of the read buffer and the decompression buffer (if needed).
skipCRC32Optional flag to skip calculation of the CRC32 checksum to improve performance.
progressA progress object that can be used to track or cancel the extract operation.
consumerA closure that consumes contents of
EntryasDatachunks.Return Value
The checksum of the processed content or 0 if the
skipCRC32flag was set totrue.. -
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: Int = defaultWriteChunkSize, progress: Progress? = nil) throwsParameters
pathThe path that is used to identify an
Entrywithin theArchivefile.baseURLThe base URL of the resource to add. The
baseURLcombined withpathmust form a fully qualified file URL.compressionMethodIndicates the
CompressionMethodthat should be applied toEntry. By default, no compression will be applied.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.
-
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, fileURL: URL, compressionMethod: CompressionMethod = .none, bufferSize: Int = defaultWriteChunkSize, progress: Progress? = nil) throwsParameters
pathThe path that is used to identify an
Entrywithin theArchivefile.fileURLAn absolute file URL referring to the resource to add.
compressionMethodIndicates the
CompressionMethodthat should be applied toEntry. By default, no compression will be applied.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: Int64, modificationDate: Date = Date(), permissions: UInt16? = nil, compressionMethod: CompressionMethod = .none, bufferSize: Int = 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. By default, no compression will be applied.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: Int = 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.
-
Declaration
Swift
func addEntry(with path: String, relativeTo baseURL: URL, compressionMethod: CompressionMethod = .none, bufferSize: UInt32, progress: Progress? = nil) throws -
Declaration
Swift
func addEntry(with path: String, fileURL: URL, compressionMethod: CompressionMethod = .none, bufferSize: UInt32, progress: Progress? = nil) throws -
addEntry(with:type: uncompressedSize: modificationDate: permissions: compressionMethod: bufferSize: progress: provider: ) Declaration
Swift
func addEntry(with path: String, type: Entry.EntryType, uncompressedSize: UInt32, modificationDate: Date = Date(), permissions: UInt16? = nil, compressionMethod: CompressionMethod = .none, bufferSize: Int = defaultWriteChunkSize, progress: Progress? = nil, provider: (_ position: Int, _ size: Int) throws -> Data) throws -
Declaration
Swift
func remove(_ entry: Entry, bufferSize: UInt32, progress: Progress? = nil) throws
Archive Class Reference