英文:
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
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论