3

SSZipArchive用来提取文件的内容,我从 Internet 下载并保存在Documents. 我正在下载的资源是 zip 文件的形式,我将在成功下载后解压缩。

@IBAction func downloadData(sender: UIButton)
{       if let myAudioDataFromUrl = NSData(contentsOfURL: audioUrl){
                // after downloading your data you need to save it to your destination url
                if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) {
                    print("file saved")
                    printTimestamp()
                    let folderName = "Aspire  Brochure"//audioUrl.lastPathComponent!.stringByReplacingOccurrencesOfString(".zip", withString: "")
                    let destination = documentsUrl.URLByAppendingPathComponent(folderName)
                    let fileManager = NSFileManager.defaultManager()
                    print("This is the folderName \(destinationUrl)")
                    print("This is the destination \(destination)")
                    let success = fileManager.fileExistsAtPath(destination.absoluteString) as Bool
                    if success == false {
                        do {
                            print("it is here")
                            try! fileManager.createDirectoryAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), withIntermediateDirectories: true, attributes: nil)
                            print("it hascome here  ")
                        }
                    }
                 //   let success = fileManager.fileExistsAtPath(destPath) as Bool
                    print("trying to unzip the file")

                    //let fileManager = NSFileManager.defaultManager()
                    let checkFile = NSFileManager.defaultManager().fileExistsAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""))
                    print("this is the check value response \(checkFile)")
                    var tmp = SSZipArchive.unzipFileAtPath(destinationUrl.absoluteString, toDestination: destination.absoluteString )
                    //var tmp = SSZipArchive.unzipFileAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), toDestination: destination.absoluteString )
                    print("unzipped the file \(tmp)")

                    } else {
                        print("error saving file")
                    }
                }
            }

该文件正在创建中,但没有任何内容被提取到其中。

4

1 回答 1

11

今天遇到这个我花了几个小时才弄清楚。找不到 zip 文件时SSZipArchive返回。false这可能是因为您返回的 url 是[NSURL absoluteString];开头file://的。只需调用[NSURL path];url,它应该可以工作

于 2016-06-02T13:08:50.043 回答