2017-10-09 58 views
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) 
+0

您可以使用回叫像完成處理 –

+0

函數u可以給一個示例代碼或鏈接怎麼用它呢? –

回答

0

URLSession處理兩種類型的回調機制工作解壓它代碼:

  • 完成處理 - 這是你的代碼是用
  • URLSession代表

具體回答你的問題,完成處理您在上面的代碼中編寫的代碼將在下載完成時調用。或者,如果你想要做同樣的委託方法,代碼應該是這樣的:

import Foundation 
import Dispatch //you won't need this in your app 

public class DownloadTask : NSObject { 

    var currDownload: Int64 = -1 

    func download(urlString: String) { 
     let config = URLSessionConfiguration.default 
     let session = URLSession(configuration: config, delegate: self, delegateQueue: nil) 

     let url = URL(string: urlString) 
     let task = session.downloadTask(with: url!) 
     task.resume() 
    } 
} 


extension DownloadTask : URLSessionDownloadDelegate { 

    //this delegate method is called everytime a block of data is received  
    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, 
         totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) -> Void { 
     let percentage = (Double(totalBytesWritten)/Double(totalBytesExpectedToWrite)) * 100 
     if Int64(percentage) != currDownload { 
      print("\(Int(percentage))%") 
      currDownload = Int64(percentage) 
     } 
    } 

    //this delegate method is called when the download completes 
    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { 
     //You can copy the file or unzip it using `location` 
     print("\nFinished download at \(location.absoluteString)!") 
    } 

} 

let e = DownloadTask() 
e.download(urlString: "https://swift.org/LICENSE.txt") 
dispatchMain() //you won't need this in your app