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 more

    Declaration

    Swift

    public enum ArchiveError : Error
  • The access mode for an Archive.

    See more

    Declaration

    Swift

    public enum AccessMode : UInt
  • url

    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

    url

    File URL to the receivers backing file.

    mode

    Access 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 nil if 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
  • Declaration

    Swift

    public func makeIterator() -> AnyIterator<Entry>
  • Retrieve the ZIP Entry with the given path from 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 Entry with the given path.

    Declaration

    Swift

    public subscript(path: String) -> Entry? { get }

    Parameters

    path

    A relative file path identifiying the corresponding Entry.

    Return Value

    An Entry with the given path. 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) throws

    Parameters

    path

    The path that is used to identify an Entry within the Archive file.

    baseURL

    The base URL of the Entry to add. The baseURL combined with path must form a fully qualified file URL.

    compressionMethod

    Indicates the CompressionMethod that should be applied to Entry.

    bufferSize

    The maximum size of the write buffer and the compression buffer (if needed).

    progress

    A 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 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) throws

    Parameters

    path

    The path that is used to identify an Entry within the Archive file.

    type

    Indicates the Entry.EntryType of the added content.

    uncompressedSize

    The uncompressed size of the data that is going to be added with provider.

    modificationDate

    A Date describing the file modification date of the Entry. Default is the current Date.

    permissions

    POSIX file permissions for the Entry. Default is 0o644 for files and symlinks and 0o755 for directories.

    compressionMethod

    Indicates the CompressionMethod that should be applied to Entry.

    bufferSize

    The maximum size of the write buffer and the compression buffer (if needed).

    progress

    A progress object that can be used to track or cancel the add operation.

    provider

    A closure that accepts a position and a chunk size. Returns a Data chunk.

  • Remove a ZIP Entry from the receiver.

    Throws

    An error if the Entry is malformed or the receiver is not writable.

    Declaration

    Swift

    public func remove(_ entry: Entry, bufferSize: UInt32 = defaultReadChunkSize, progress: Progress? = nil) throws

    Parameters

    entry

    The Entry to remove.

    bufferSize

    The maximum size for the read and write buffers used during removal.

    progress

    A 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 entry from the receiver.

    Declaration

    Swift

    public func totalUnitCountForRemoving(_ entry: Entry) -> Int64

    Parameters

    entry

    The 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 entry from the receiver.

    Declaration

    Swift

    public func totalUnitCountForReading(_ entry: Entry) -> Int64

    Parameters

    entry

    The 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 url to the receiver.

    Declaration

    Swift

    public func totalUnitCountForAddingItem(at url: URL) -> Int64

    Parameters

    entry

    The entry that will be removed.

    Return Value

    The number of the work units.