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

标签 ios swift ssziparchive

 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/

相关文章:

ios - react-native-video onLoad onProgress 不工作 IOS

swift - 构造函数调用的可选链接?

ios - 的值没有成员

ios - 每次我尝试添加 ZipArchive 或 ssziparchive 时,Xcode 中都会出现很多错误

objective-c - Objective C : Cell. ImageView.image 大小不一致

ios - 访问自定义 cocoapod 内的 pod

ios - Swift 如何修复 Target Membership 的错误?

ios - 从 App Delegate 打开导航 Controller

ios - 无法加载 NSData(contentsOfFile : file path) from mobile or simulator filesystem, 但它适用于 OS X 文件系统

ios - Xcode:安装框架时链接器命令错误