代码之家  ›  专栏  ›  技术社区  ›  Adam

如何使用Alamofire和SSZipArchive从URL解压文件

  •  2
  • Adam  · 技术社区  · 6 年前
     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")
    

    我想这是正确的网址

    为什么解压缩失败?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Bhavin Kansagara    6 年前

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

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

    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
    }
    

    尝试分享结果。