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") }