【问题标题】:Path to downloaded file with Alamofire4 (Swift3)使用 Alamofire 4 (Swift 3) 下载文件的路径
【发布时间】:2016-11-02 03:23:34
【问题描述】:

我用 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 文件路径有误吗?

【问题讨论】:

    标签: ios swift3 alamofire ssziparchive


    【解决方案1】:

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

        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
        }
    

    【讨论】:

      【解决方案2】:

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

       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!)
      

      【讨论】:

      • 我已经尝试过了,但是尝试抛出有这个错误:Domain=NSCocoaErrorDomain Code=512 "Impossible to save file «Images » in folder «Documents »." UserInfo={NSFilePath=/Users/username/Library/Developer/CoreSimulator/Devices/3D7DE09C-CA8B-4E32-A048-2B33887CE47B/data/Containers/Data/Application/xxxx/Documents/Images, NSUnderlyingError=0x60800005bfc0 {Error Domain=NSPOSIXErrorDomain代码=20“不是目录”}}:文件/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift,第178行为什么它不是目录?
      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多