使用SSZipArchive解压缩下载的zip文件

时间:2018-12-14 06:39:55

标签: swift epub3 ssziparchive

我用来下载这本书的代码是..
 //创建要下载的源文件的URL         让fileURL = URL(string:myFileURL)

    let sessionConfig = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfig)

    let request = URLRequest(url:fileURL!)

    let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
        if let tempLocalUrl = tempLocalUrl, error == nil {
            // Success
            if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                print("Successfully downloaded. Status code: \(statusCode)")

                DispatchQueue.main.async {
                    self.toast(msg: "Download completed")
                    self.activityIndicator.stopAnimating()
                    self.activityIndicator.isHidden = true

                }

            }

            do {
                try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
            } catch (let writeError) {
                print("Error creating a file \(destinationFileUrl) : \(writeError)")
            }

        } else {
            print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
        }

之后,我尝试使用代码从destinationFileUrl解压缩epub文件

let status = SSZipArchive.unzipFile(atPath: destinationFileUrl , toDestination:destPath, delegate:self)

状态值返回false,因此输出文件夹仅包含META-INF文件夹,没有任何项目

我如何正确解压缩epub

1 个答案:

答案 0 :(得分:0)

请参考--Not getting eject file after unzipping downloaded file URL

     var filepath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
                filepath += "/\(UUID().uuidString)"
                let url = URL(fileURLWithPath: filepath)
                do {
                    try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
                    let done = SSZipArchive.unzipFile(atPath: path!, toDestination: url.path)
                    if done{
                        let items = try FileManager.default.contentsOfDirectory(atPath: url.path)
                        print(items)
                   }
                } catch let error as NSError{
                    print(error)
                }