Download zip file and unzip it using Alamofire in Swift

huangapple go评论102阅读模式
英文:

Download zip fil and unzip it using Alamofire in Swift

问题

I have a zip file of assets in AWS S3 bucket. I want to download that zip file using Alamofire and unzip and save it on my device. I am using Swift. Please help.

  1. func downloadFile(url: URL, completionHandler: @escaping (NSDictionary?, Error?) -> Void) {
  2. let destinationPath: DownloadRequest.Destination = { _, _ in
  3. let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  4. let fileURL = documentsURL.appendingPathComponent("Assets.zip")
  5. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  6. }
  7. AF.download(url, to: destinationPath).responseData { response in
  8. print(response)
  9. switch response.result {
  10. case .success(_):
  11. print("Successfully Downloaded")
  12. case .failure(_):
  13. print("Download failed")
  14. }
  15. }
  16. }

这是您提供的代码的翻译部分。

英文:

I have a zip file of assets in AWS S3 bucket. I want to download that zip file using Alamofire and unzip and save in my device. I am using Swift. Please help.

  1. func downloadFile(url : URL, completionHandler: @escaping(NSDictionary?, Error?) -> Void){
  2. let destinationPath : DownloadRequest.Destination = { _, _ in
  3. let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
  4. let fileURL = documentsURL.appendingPathComponent("Assets.zip")
  5. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  6. }
  7. AF.download(url, to: destinationPath).responseData { response in
  8. print(response)
  9. switch response.result{
  10. case .success(_):
  11. print ("sussessfully Downloaded")
  12. case .failure(_):
  13. print("download failed")
  14. }
  15. }
  16. }

答案1

得分: 1

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

然后

  1. import Zip

下载函数调用

  1. self.downloadPdf(pdfReport: webLink, savePathURL: finalDestinationUrl) { (pdfFilePath, status) in
  2. if FileManager.default.fileExists(atPath: pdfFilePath){
  3. try? Zip.unzipFile(pathUrl, destination: finalUrl ?? validUrl, overwrite: true, password: nil, progress: { (progress) in
  4. print(progress)
  5. })
  6. }
  7. }

下载函数定义

  1. func downloadPdf(pdfReport: String, savePathURL: URL, completionHandler:@escaping(String, Bool)->()){
  2. let downloadUrl: String = pdfReport
  3. let destinationPath: DownloadRequest.Destination = { _, _ in
  4. return (savePathURL, [.removePreviousFile, .createIntermediateDirectories])
  5. }
  6. debugPrint("downloadUrl=>",downloadUrl)
  7. AF.download(downloadUrl, to: destinationPath)
  8. .downloadProgress { progress in
  9. }
  10. .responseData { response in
  11. print("response: \(response)")
  12. switch response.result{
  13. case .success:
  14. if response.error == nil, let filePath = response.fileURL?.path {
  15. completionHandler(filePath, true)
  16. }
  17. break
  18. case .failure:
  19. completionHandler("", false)
  20. break
  21. }
  22. }
  23. }
英文:

First of all need to install pod
https://cocoapods.org/pods/Zip

Then

  1. import Zip

Download function call

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

Download function definition

  1. func downloadPdf(pdfReport: String, savePathURL: URL, completionHandler:@escaping(String, Bool)->()){
  2. let downloadUrl: String = pdfReport
  3. let destinationPath: DownloadRequest.Destination = { _, _ in
  4. return (savePathURL, [.removePreviousFile, .createIntermediateDirectories])
  5. }
  6. debugPrint("downloadUrl=>",downloadUrl)
  7. AF.download(downloadUrl, to: destinationPath)
  8. .downloadProgress { progress in
  9. }
  10. .responseData { response in
  11. print("response: \(response)")
  12. switch response.result{
  13. case .success:
  14. if response.error == nil, let filePath = response.fileURL?.path {
  15. completionHandler(filePath, true)
  16. }
  17. break
  18. case .failure:
  19. completionHandler("", false)
  20. break
  21. }
  22. }
  23. }

huangapple
  • 本文由 发表于 2023年5月17日 19:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定