我用Swift制作了一个游戏,但是我需要在我的网站上下载.zip文件,并在游戏中使用其中的内容(图像)。 实际上,我下载了这个.zip文件并将其存储在文档中,我需要将文件解压缩到同一个文档文件夹中。
我尝试了marmelroy的Zip、iOS 9压缩模块和tidwall的DeflateSwift,但都没有起作用。如果它与iOS 8兼容将会很好。
我最近发布了一个基于Swift的原生框架,它允许您创建、读取和更新ZIP存档文件: ZIP Foundation。
它在内部使用libcompression来获得出色的压缩性能。
解压缩文件基本上只需要一行代码:
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
一个带有一些上下文的完整示例看起来像这样:
let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}
GitHub 上的README 包含更多信息。所有公共方法还可以通过 Xcode 快速帮助获得完整文档。
我还在这里写了一篇关于性能特征的博客文章。
Swift
编写非常简单。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")
}
如果Minizip不起作用,您可以尝试使用ZipArchive,它不是用Swift编写的,而是用Objective-C编写的。
我使用SSZipArchive和桥接头使其工作:
这段代码将文件下载到文档目录并解压缩zip文件
别忘了安装ZIPFoundation
import ZIPFoundation
func downloadAndUnzip(from urlString: String, toFolder folderName: String) {
guard let url = URL(string: urlString) else { return }
let session = URLSession.shared
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderURL = documentsDirectoryURL.appendingPathComponent(folderName, isDirectory: true)
let fileURL = folderURL.appendingPathComponent(url.lastPathComponent)
do { //Creates folder.
try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
} catch {
debugPrint("Could not create directory: \(error.localizedDescription)")
}
let task = session.downloadTask(with: url) { (tempLocalURL, response, error) in
guard let tempLocalURL = tempLocalURL, error == nil else { return }
do { //Moves data to created folder.
try FileManager.default.moveItem(at: tempLocalURL, to: fileURL)
debugPrint("ZIP File saved to: \(fileURL.path)")
do { //Try to unzip the file.
try FileManager.default.unzipItem(at: fileURL, to: folderURL)
do { //Try to delete zip file.
try FileManager.default.removeItem(at: fileURL)
debugPrint("ZIP file deleted at:", fileURL)
} catch {
debugPrint("Could not delete ZIP file with error: \(error.localizedDescription)", fileURL)
}
debugPrint("Unzip completed!", fileURL)
} catch {
debugPrint("Extraction of ZIP archive failed with error:\(error)")
}
} catch {
debugPrint("Could not move item: \(error.localizedDescription)")
}
}
task.resume()
}
点击“添加包”并粘贴 github.com/weichsel/ZIPFoundation.git
FileManager().unzipItem(at: fromUrl, to: untoUrl)
。
就是这样。
你完成了。
来一杯啤酒吧。
以下是其他典型代码的完整示例:
let fromUrl = Bundle.main.url(forResource: "yourZipFile", withExtension: "zip")!
let untoUrl = FileManager().temporaryDirectory.appendingPathComponent("temp")
do {
try FileManager.default.removeItem(at: untoUrl)
try FileManager().createDirectory(at: untoUrl, withIntermediateDirectories: true)
try FileManager().unzipItem(at: fromUrl, to: untoUrl, skipCRC32: true)
}
catch { print(error) }
do {
let items = try FileManager.default
.contentsOfDirectory(at: untoUrl, includingPropertiesForKeys: nil)
.sorted{ $0.lastPathComponent < $1.lastPathComponent }
for item in items { print(item.path) }
blah = items.compactMap{ blah($0) }
}
catch { print(error) }
重要提示:需要第三方库
使用这个库,你可以使用这行代码:
let unzipDirectory= try Zip.quickUnzipFile(filePath) //to unZip your folder.
unzipItem()
似乎不可用。 - pinglock