SwiftZip Documentation Beta

Class Zip​Archive

public class ZipArchive

An read-only archive.

%3 ZipArchive ZipArchive ZipMutableArchive ZipMutableArchive ZipMutableArchive->ZipArchive

Nested Types

ZipArchive.AddFileFlags

A set of flags to be used with ZipArchive.addFile.

ZipArchive.EntryCollection

A collection of read-only entries in the archive.

ZipArchive.FDOpenFlags

A set of flags to be used with ZipArchive.init(fd:flags:).

ZipArchive.LocateFlags

A set of flags to be used with ZipArchive name-based APIs: locate, stat and open.

ZipArchive.OpenFlags

A set of flags to be used with ZipArchive.init.

ZipArchive.Version

A version of the archive.

Subclasses

ZipMutableArchive

An mutable archive.

Initializers

init(path:​flags:​)

public convenience init(path: String, flags: OpenFlags = []) throws

Opens the zip archive specified by path and sets up an instance, used to manipulate the archive.

Parameters

path String

path to open

flags Open​Flags

open flags, defaults to []

init(url:​flags:​)

public convenience init(url: URL, flags: OpenFlags = []) throws

Opens the zip archive specified by URL and sets up an instance, used to manipulate the archive.

Parameters

url URL

URL to open

flags Open​Flags

open flags, defaults to []

init(source:​flags:​)

public convenience init(source: ZipSource, flags: OpenFlags = []) throws

Opens a zip archive encapsulated by the ZipSource and sets up an instance, used to manipulate the archive.

Parameters

source Zip​Source

source to open

flags Open​Flags

open flags, defaults to []

init(fd:​flags:​)

public convenience init(fd: Int32, flags: FDOpenFlags = []) throws

The zip archive specified by the open file descriptor fd is opened and an instance, used to manipulate the archive, is comfigured. In contrast to other initializers, the archive can only be opened in read-only mode. The fd argument may not be used any longer after calling ZipArchive.init(fd:flags:).

Upon successful completion fd should not be used any longer, nor passed to close(2). In the error case, fd remains unchanged.

Parameters

fd Int32

file descriptor to use

flags FDOpen​Flags

open flags, defaults to []

Methods

discard()

public func discard()

Closes archive and frees the memory allocated for it. Any changes to the archive are not written to disk and discarded. The ZipArchive object is invalidated and must not be used after call to dsicard().

set​Default​Password(_:​)

public func setDefaultPassword(_ password: String?) throws

Sets the default password used when accessing encrypted files. If password is nil, the default password is unset. If you prefer a different password for single files, pass password to ZipArchive.open or ZipEntry.open instead. Usually, however, the same password is used for every file in an zip archive.

Parameters

password String?

the password to be used

get​Comment(decoding​Strategy:​version:​)

public func getComment(decodingStrategy: ZipStringDecodingStrategy = .guess, version: Version = .current) throws -> String

Returns the comment for the entire zip archive.

Parameters

decoding​Strategy Zip​String​Decoding​Strategy

string decoding strategy, defaults to .guess

version Version

archive version to use, defaults to .current

get​Raw​Comment(version:​)

public func getRawComment(version: Version = .current) throws -> Data

Returns the unmodified archive comment as it is in the ZIP archive.

Parameters

version Version

archive version to use, defaults to .current

get​Entry​Count(version:​)

public func getEntryCount(version: Version = .current) throws -> Int

Returns the number of files in archive.

Parameters

version Version

archive version to use, defaults to .current

get​Entry(index:​version:​)

public func getEntry(index: Int, version: Version = .current) throws -> ZipEntry

Retrieves archive entry by index.

Parameters

index Int

index of entry to retrieve

locate(filename:​locate​Flags:​version:​)

public func locate(filename: String, locateFlags: LocateFlags = [], version: Version = .current) throws -> ZipEntry

Returns the index of the file named filename in archive. If archive does not contain a file with that name, an error is thrown.

Parameters

filename String

entry name to locate

locate​Flags Locate​Flags

lookup options, defaults to []

version Version

archive version to use, defaults to .current

stat(filename:​locate​Flags:​version:​)

public func stat(filename: String, locateFlags: LocateFlags = [], version: Version = .current) throws -> ZipStat

Obtains information about the file named filename in archive.

Parameters

filename String

entry name

locate​Flags Locate​Flags

lookup options, defaults to []

version Version

archive version to use, defaults to .current

open(filename:​flags:​locate​Flags:​version:​password:​)

public func open(filename: String, flags: ZipEntry.OpenFlags = [], locateFlags: LocateFlags = [], version: Version = .current, password: String? = nil) throws -> ZipEntryReader

Opens the file named filename in archive using the password given in the password argument.

Parameters

filename String

entry name to open

flags Zip​Entry.​Open​Flags

open flags, defaults to []

locate​Flags Locate​Flags

lookup options, defaults to []

version Version

archive version to use, defaults to .current

password String?

optional password to decrypt the entry

entries(version:​)

public func entries(version: Version = .current) -> EntryCollection

Exposes archive entries as a Swift Collection