0
我有,我有,從網址,一次下載完成,我需要把它解壓在異步方式下載的zip文件的場景從網址下載。這裏的問題是FileManager.default.copyItem需要一段時間,因此我不能立即解壓縮文件。下面是下載zip文件代碼:斯威夫特 - 如何知道什麼時候該文件是成功地利用FileManager.default.copyItem
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()
}
作爲一個初學者,我想知道的是有在迅速提供任何回調,可以告訴我的文件下載,並可用於解壓。我正在使用SSZipArchive庫來解壓縮文件。
下面是使用
SSZipArchive.unzipFile(atPath: path, toDestination: destinationpath)
您可以使用回叫像完成處理 –
函數u可以給一個示例代碼或鏈接怎麼用它呢? –