首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提高使用ZIPFoundation解压缩文件的速度?

如何提高使用ZIPFoundation解压缩文件的速度?
EN

Stack Overflow用户
提问于 2020-08-05 23:19:59
回答 1查看 681关注 0票数 0

我正在下载一个.zip文件,其中包含一个使用Alamofire纹理的3D模型。文件大小目前为20 is。使用ZIPFoundation后,解压缩文件所需的时间是50~50。文档表明,解压缩接近20 1s的文件应该使用<1s。然而,我没有取得这些成果。下面是完成文件下载请求和解压缩的代码:

代码语言:javascript
复制
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)
        }
    }
}

我下载和解压缩文件的方法有什么问题吗?还是对库的限制?如果是这样的话,有人能推荐另一个加速这个过程的库吗?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-06 05:04:20

解压缩存档的代码看起来不错。

在Swift中,优化的构建和非优化的构建有很大的性能差异。当您切换到“版本”构建时,解压缩需要多长时间?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63274798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档