FileManager

extension FileManager
  • Zips the file or directory contents at the specified source URL to the destination URL.

    If the item at the source URL is a directory, the directory itself will be represented within the ZIP Archive. Calling this method with a directory URL file:///path/directory/ will create an archive with a directory/ entry at the root level. You can override this behavior by passing false for shouldKeepParent. In that case, the contents of the source directory will be placed at the root of the archive.

    Throws

    Throws an error if the source item does not exist or the destination URL is not writable.

    Declaration

    Swift

    public func zipItem(at sourceURL: URL, to destinationURL: URL,
                        shouldKeepParent: Bool = true, compressionMethod: CompressionMethod = .none,
                        progress: Progress? = nil) throws

    Parameters

    sourceURL

    The file URL pointing to an existing file or directory.

    destinationURL

    The file URL that identifies the destination of the zip operation.

    shouldKeepParent

    Indicates that the directory name of a source item should be used as root element within the archive. Default is true.

    compressionMethod

    Indicates the CompressionMethod that should be applied. By default, zipItem will create uncompressed archives.

    progress

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

  • Unzips the contents at the specified source URL to the destination URL.

    Throws

    Throws an error if the source item does not exist or the destination URL is not writable.

    Declaration

    Swift

    public func unzipItem(at sourceURL: URL, to destinationURL: URL, skipCRC32: Bool = false,
                          progress: Progress? = nil, preferredEncoding: String.Encoding? = nil) throws

    Parameters

    sourceURL

    The file URL pointing to an existing ZIP file.

    destinationURL

    The file URL that identifies the destination directory of the unzip operation.

    skipCRC32

    Optional flag to skip calculation of the CRC32 checksum to improve performance.

    progress

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

    preferredEncoding

    Encoding for entry paths. Overrides the encoding specified in the archive.