I am downloading a .zip file containing a 3D model with textures using Alamofire. The file size is currently 20mb. After using ZIPFoundation, the time it takes to unzip the file is ~50s. The documentation indicates that unzipping a file close to 20mb should take <1s. However, I am not achieving these results. Here is my code that completes the download request and unzipping of the files:
func downloadRequest(url: String, method: HTTPMethod, parameters: [String:String]?, headers: [String:String]?, destFileName: String, completion: @escaping (URL?) -> Void) {
//Initial parameters
let fullDestName = destFileName + ".zip"
let fm = FileManager.default
guard let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
//Initialise temporary download location
let destination: DownloadRequest.DownloadFileDestination = { _,_ in
let temporaryDownloadDest = documentsDirectory.appendingPathComponent(fullDestName)
return (temporaryDownloadDest, [.createIntermediateDirectories,.removePreviousFile])
}
//Call API to begin download request
Alamofire.download(url, method: method, parameters: parameters, headers: headers, to: destination).response { (response) in
let responseError = response.error
if responseError == nil {
print("File successfully downloaded")
if let downloadFilePath = response.destinationURL?.path { //.....Documents/name.zip
let sourceURL = URL(fileURLWithPath: downloadFilePath)
let destinationURL = documentsDirectory.appendingPathComponent(destFileName) //....Documents/name
DispatchQueue.global().async { //Failing to do this will lock up main thread
do {
try fm.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fm.unzipItem(at: sourceURL, to: destinationURL) // <------ UNZIPPING THE FILE HERE
completion(destinationURL)
} catch {
print("Extraction of ZIP archive failed with error: \(error)")
completion(nil)
}
}
} else {
print("file path was not found")
completion(nil)
}
} else {
print("There was an error: \(responseError!)")
completion(nil)
}
}
}
Is there something wrong with my approach of downloading and unzipping the files, or is it a limitation with the library? If so, can anyone recommend another library that speeds up the process? Thanks in advance
The code to unzip the archive looks good.
There is a huge performance difference between optimized and non-optimized builds in Swift. How long does the unzip take when you switch to a "Release" build?