Zip - zipping和unzipping文件的Swift框架

jopen 8年前發布 | 14K 次閱讀 Apple Swift開發 ZIP

Zip - Zip and unzip files in Swift

Zip

Zip - zipping和unzipping文件的Swift框架。 Simple and quick to use. Built on top of minizip.

Usage

Import Zip at the top of the Swift file.

import Zip

Quick functions

The easiest way to use Zip is through quick functions. Both take local file paths as NSURLs, throw if an error is encountered and return an NSURL to the destination if successful.

do {
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
    let unzipDirectory = try Zip().quickUnzipFile(filePath) // Unzip
    let zipFilePath = try Zip().quickZipFiles([filePath], fileName: "archive") // Zip
}
catch {
  print("Something went wrong")
}

Advanced Zip

For more advanced usage, Zip has functions that let you set custom destination paths, work with password protected zips and use a progress handling closure. These function throw if there is an error but don't return.

do {
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

    try Zip().unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
        print(progress)
    }) // Unzip

    let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip")
    try Zip().zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
        print(progress)
    }) //Zip

}
catch {
  print("Something went wrong")
}

Setting up with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Format into your Xcode project using Carthage, specify it in your Cartfile:

github "marmelroy/Zip"

Setting up with CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
pod 'Zip', '~> 0.1'

項目地址: https://github.com/marmelroy/Zip

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!