歡迎加入QQ討論群258996829

Swift 壓縮和解壓文件的框架 Zip

發(fā)布時間:2017-03-24 21:59  回復:0  查看:7834  感興趣:82  贊:0   最后回復:2017-03-24 21:59  

Zip是一個用于壓縮和解壓文件的Swift框架。

快速上手

通過quickUnzipFile函數(shù)和quickZipFiles函數(shù)可以很方便的解壓縮文件。兩者都是用NSURLs類型的路徑為參數(shù),發(fā)生錯誤將拋出異常,成功則返回NSURL類型。

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

高級用法

Zip提供了相關(guān)函數(shù),可以自定義目標路徑、設(shè)置密碼、獲取進度。這些方法出錯時會拋出異常,成功時沒有返回值。

do {
    let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
    let documentsDirectory = FileManager.default.urls(for:.documentDirectory, in: .userDomainMask)[0]
    try Zip.unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
        print(progress)
    }) // Unzip

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

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


相關(guān)開源代碼

您還未登錄,請先登錄

熱門帖子

最新帖子

?