1

I am using SSZipArchive to unzip files from a Url (http://www.colorado.edu/conflict/peace/download/peace.zip). For this I am using the function :

SSZipArchive.unzipFile(atPath: path, toDestination: documentsPath, progressHandler: {  
  1. Is it possible to extract files from Url using SSZipArchive ?
  2. Is yes, how do I pass Url in atPath ?
Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

0

You can't directly unzip without downloading the file from the URL.
This is how I do this,

func downLoad() {
    let url : String = "http://www.colorado.edu/conflict/peace/download/peace.zip"
    var localPath: NSURL?
    Alamofire.download(.GET,url,
        destination: { (temporaryURL, response) in
            let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
            let pathComponent = response.suggestedFilename
            localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
            return localPath!
    })
        .response { (request, response, _, error) in
            SwiftEventBus.post("DownloadedSuccessfully", sender: localPath!)
    }  
}

After downloading the content, unzip it where you want to.

SwiftEventBus.onMainThread(self, name:"DownloadedSuccessfully")
    {
        result in

        let resultStatus = result.object as! NSURL

        guard let zipPath: String = resultStatus.path! as String else {
            return
        }
        guard let unZipPath = unzipPath() else {
            return
        }

        let success = SSZipArchive.unzipFileAtPath(zipPath, toDestination: unZipPath)
        print(unZipPath)
        if !success {
            return
        }
    }

I unzipped the file and stored it in the document directory

func unzipPath() -> String? {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let url = NSURL(fileURLWithPath: path)
    do {
        try NSFileManager.defaultManager().createDirectoryAtURL(url, withIntermediateDirectories: true, attributes: nil)
    } catch {
        return nil
    }
    if let path = url.path {
        return path
    }
    return nil
}

From there you can access the data of the file. Here I used third party frameworks Alamofire and swiftEventBus. Hope this works for you.

Praveen Kumar
  • 298
  • 2
  • 15