使用SSZipArchive解压缩文件而不提取文件的内容

时间:2016-06-01 09:14:08

标签: ios swift unzip ssziparchive

我正在使用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")
                    }
                }
            }

正在创建文件,但不会将任何内容提取到文件中。

1 个答案:

答案 0 :(得分:9)

今天跑到这里花了几个小时来搞清楚。 SSZipArchive无法找到zip文件时返回false。这可能是因为您要返回的网址是[NSURL absoluteString];,其开头有file://。只需在网址上调用[NSURL path];即可使用