gpt4 book ai didi

ios - Swift 如何使用 Alamofire 和 SSZipArchive 从 URL 解压缩文件

转载 作者:可可西里 更新时间:2023-11-01 01:08:35 39 4
gpt4 key购买 nike

 func downLoad(fileName:String) {
let urlString : String = "\(myurl)\(fileName)"
var localPath: NSURL?
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

Alamofire.download(urlString, method: .get, encoding: JSONEncoding.default, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
print("Progress: \(progress.fractionCompleted)")
}
.validate { request, response, temporaryURL, destinationURL in
// Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
print(response.destinationURL?.path)
print(response.destinationURL?.absoluteString)
let unzipDirectory = self.unzipPath(fileURL:fileName)

let success = SSZipArchive.unzipFile(atPath: (response.destinationURL?.path)!, toDestination: unzipDirectory!)
print(success)
if !success {
return
}


}
}


func unzipPath(fileName:String) -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let pathComponent = url.appendingPathComponent("test\(fileName)")
do {
try FileManager.default.createDirectory(at: pathComponent!, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return pathComponent?.absoluteString
}

我在 response.destinationURL 中得到正确的路径

但成功是假的

我试过 atPath :response.destinationURL?.path

response.destinationURL?.absoluteString

但也失败了

我做错了什么

destinationURL?.path 返回这个

Optional("/Users/MyUser/Library/Developer/CoreSimulator/Devices/3FBAD207-E5AB-4FC1-8199-2269A1249D97/data/Containers/Data/Application/CB1C2EF5-3100-430B-B869-774C09B8EA7F/Documents/testFile.zip")


response.destinationURL?.absoluteString

返回这个

Optional("file:///Users/MyUser/Library/Developer/CoreSimulator/Devices/3FBAD207-E5AB-4FC1-8199-2269A1249D97/data/Containers/Data/Application/CB1C2EF5-3100-430B-B869-774C09B8EA7F/Documents/testFile.zip")

我认为这是正确的网址

为什么解压失败?

最佳答案

通过查看您的代码,您用于源代码的路径是完美的。

对于目标路径,尝试将其更新为

func unzipPath(fileName:String) -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let pathWithComponent = path.appendingPathComponent("test\(fileName)")
do {
try FileManager.default.createDirectory(atPath: pathWithComponent, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return pathWithComponent
}

尝试并分享结果。

关于ios - Swift 如何使用 Alamofire 和 SSZipArchive 从 URL 解压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550713/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com