一个用于压缩和解压缩文件的 Swift 框架。简单易用,速度快。构建于 minizip 之上。
在 Swift 文件的顶部导入 Zip。
import Zip
使用 Zip 最简单的方法是通过快速函数。它们都接受本地文件路径作为 NSURL,如果遇到错误则抛出,如果成功则返回目标文件的 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 提供了允许您设置自定义目标路径、使用密码保护的 zip 文件以及使用进度处理闭包的函数。这些函数如果出现错误会抛出,但不会返回任何值。
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")
}
Zip 开箱即用地支持“.zip”和“.cbz”文件。要支持其他 zip 衍生文件扩展名
Zip.addCustomFileExtension("file-extension-here")
要将 Zip 与 Swift Package Manager 一起使用,请将其添加到您 package 的依赖项中
.package(url: "https://github.com/marmelroy/Zip.git", .upToNextMinor(from: "2.1"))
source 'https://github.com/CocoaPods/Specs.git'
pod 'Zip', '~> 2.1'
要使用 Carthage 将 Zip 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "marmelroy/Zip" ~> 2.1