ios 下载zip文件并在Swift中使用Alamofire解压缩

nqwrtyyt  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(197)

我在AWS S3存储桶中有一个资产的zip文件。我想使用Alamofire下载该zip文件并解压缩并保存在我的设备中。我用的是Swift。请帮帮我

func downloadFile(url : URL, completionHandler: @escaping(NSDictionary?, Error?) -> Void){
        let destinationPath : DownloadRequest.Destination = { _, _ in
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
        let fileURL = documentsURL.appendingPathComponent("Assets.zip")
        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    AF.download(url, to: destinationPath).responseData { response in
        print(response)
        switch response.result{
        case .success(_):
            print ("sussessfully Downloaded")
        case .failure(_):
            print("download failed")
        }
    }
 }
ef1yzkbh

ef1yzkbh1#

首先需要安装pod**https://cocoapods.org/pods/Zip**
然后

import Zip

下载函数调用

self.downloadPdf(pdfReport: webLink, savePathURL: finalDestinationUrl) { (pdfFilePath, status) in
        
        if FileManager.default.fileExists(atPath: pdfFilePath){
            
            
            try? Zip.unzipFile(pathUrl, destination: finalUrl ?? validUrl, overwrite: true, password: nil, progress: { (progress) -> () in
                print(progress)
                
                
            })
            
        }
    }

下载函数定义

func downloadPdf(pdfReport: String, savePathURL: URL, completionHandler:@escaping(String, Bool)->()){
    
    let downloadUrl: String =  pdfReport
    let destinationPath: DownloadRequest.Destination = { _, _ in
        return (savePathURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    debugPrint("downloadUrl=>",downloadUrl)
    AF.download(downloadUrl, to: destinationPath)
        .downloadProgress { progress in
            
        }
        .responseData { response in
            print("response: \(response)")
            switch response.result{
            case .success:
                if response.error == nil, let filePath = response.fileURL?.path {
                    completionHandler(filePath, true)
                }
                break
            case .failure:
                completionHandler("", false)
                break
            }
            
        }
}

相关问题