我有一个场景,我必须从 url 下载一个 zip 文件,一旦下载完成,我需要以异步方式解压缩它。这里的问题是 FileManager.default.copyItem 需要一些时间,因此我无法立即解压缩文件。以下是下载 zip 文件的代码:
func saveZipFile(url: URL, directory: String) -> Void {
let request = URLRequest(url: url)
let task = URLSession.shared.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
try FileManager.default.copyItem(at: tempLocalUrl as URL, to: FileChecker().getPathURL(filename: url.lastPathComponent, directory: directory))
print("sucessfully downloaded the zip file ...........")
//unziping it
//self.unzipFile(url: url, directory: directory)
} catch (let writeError) {
print("Error creating a file : \(writeError)")
}
} else {
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
}
}
task.resume()
}
作为初学者,我想知道 swift 中是否有任何可用的回调可以告诉我文件已下载并可用于解压缩。我正在使用 SSZipArchive 库来解压缩文件。
下面是用于解压缩它的代码
SSZipArchive.unzipFile(atPath: path, toDestination: destinationpath)