unzip downloaded zip file using SSZipArchive

2019-08-27 15:39发布

问题:

the code i used for downloading the book is ..
//Create URL to the source file you want to download let 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);
        }

after that i tried to unzip the epub file from the destinationFileUrl using the code

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

the status value returns false, so the output folder only contains META-INF folder with no items

How can i unzip the epub correctly

回答1:

Please refer this -->> 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)
                }