使用Alamofire4(Swift3)下载文件的路径

时间:2016-11-02 03:23:34

标签: ios swift3 alamofire ssziparchive

我下载了一个带有Alamofire4的zip文件并用SSZipArchive解压缩但是解压缩不起作用。我不确定下载文件的路径是否适合Alamofire。

以下是代码:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
    var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    return (documentsURL, [.removePreviousFile])

}
Alamofire.download(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)
.response{ response in

   if response.error == nil {

     let filename = response.response?.suggestedFilename
     var folderDestination=response.destinationURL?.path
     folderDestination=folderDestination?.appending("/\(nameCategory)")

     archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!

     //unzip
     let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip, toDestination:folderDestination!)

     if !successUnZip {
        SpeedLog.print("Problem unzip")
     }
  }
}

显示“问题解压缩”,所以我在zip文件的路径中出错了吗?

2 个答案:

答案 0 :(得分:0)

在解压缩之前,请尝试检查所有路径是否正确:

    guard let zipPath = (folderDestination?.appending("/\(filename!)"))! else {
         print("Error: zipPath are not correct: \(zipPath)")    
         return
    }

    guard let unzipPath = folderDestination! else {
        print("Error: unzipPath are not correct: \(unzipPath)")
        return
    }

    let success = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
    if !success {
        print("Error: unzipFile operation failed")
        return
    }

答案 1 :(得分:0)

只需通过附加路径即可创建文件夹名称,您需要单独创建文件夹。这是代码试试这个!

 let filename = response.response?.suggestedFilename
 var folderDestination=response.destinationURL?.path
 folderDestination=folderDestination?.appending("/\(nameCategory)")
 try! FileManager.default.createDirectory(at: folderDestination!, withIntermediateDirectories: false, attributes: nil)

 archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!

 //unzip
 let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip,toDestination:folderDestination!)