Download zip file and unzip it using Alamofire in Swift

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

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.

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("Successfully Downloaded")
        case .failure(_):
            print("Download failed")
        }
    }
}

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

英文:

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.

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")
        }
    }
 }

答案1

得分: 1

首先需要安装 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
            }

        }
}
英文:

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

Then

import Zip

Download function call

    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)
                
                
            })
            
        }
    }

Download function definition

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
            }
            
        }
}

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:

确定