如何提高使用 ZIPFoundation 解压缩文件的速度?
How can I increase the speed of unzipping files using ZIPFoundation?
我正在使用 Alamofire 下载一个 .zip 文件,其中包含带纹理的 3D 模型。当前文件大小为 20mb。使用 ZIPFoundation 后,解压缩文件所需的时间约为 50 秒。文档表明解压缩接近 20mb 的文件应该花费 <1s。但是,我没有实现这些结果。这是我完成下载请求和文件解压缩的代码:
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)
}
}
}
是我下载解压的方法有问题,还是库的限制?如果是这样,谁能推荐另一个可以加速这个过程的图书馆?提前致谢
解压缩存档的代码看起来不错。
Swift 中的优化构建和 non-optimized 构建之间存在巨大的性能差异。当您切换到“发布”版本时,解压缩需要多长时间?
我正在使用 Alamofire 下载一个 .zip 文件,其中包含带纹理的 3D 模型。当前文件大小为 20mb。使用 ZIPFoundation 后,解压缩文件所需的时间约为 50 秒。文档表明解压缩接近 20mb 的文件应该花费 <1s。但是,我没有实现这些结果。这是我完成下载请求和文件解压缩的代码:
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)
}
}
}
是我下载解压的方法有问题,还是库的限制?如果是这样,谁能推荐另一个可以加速这个过程的图书馆?提前致谢
解压缩存档的代码看起来不错。
Swift 中的优化构建和 non-optimized 构建之间存在巨大的性能差异。当您切换到“发布”版本时,解压缩需要多长时间?