Search code examples
iosswiftunzipssziparchive

Unzip a file using SSZipArchive not extracting the contents of the file


I am using SSZipArchive to extract the contents of the file, which I am downloading from the Internet and saving in Documents. The resources I am downloading is in the form of zip file, which I will be unzipping upon successfull download.

@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")
                    }
                }
            }

The file is being created however nothing is being extracted into it.


Solution

  • Ran into this today took me a few hours to figure it out. SSZipArchive returns false when it cant find the zip file. This is probably happening because the url you are returning is a [NSURL absoluteString]; which has file:// at the beginning. Just call [NSURL path]; on the url and it should work