L
L
Leshich2015-01-30 00:06:14
Objective-C
Leshich, 2015-01-30 00:06:14

Swift/Objective-C. How to convert NSData to zip and then unpack to project folder?

let url: NSURL = NSURL(string: "http://.../data.zip")!
let sharedSession = NSURLSession.sharedSession()
let downloadTask:NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(url, completionHandler: { (location: <#NSURL!#>, response: <#NSURLResponse!#>, error:<#NSError!#>) -> Void in nil
  let dataObject = NSData(contentsOfURL: location)
  // ???
})
downloadTask.resume()

In the code, I received it using url zip in the form of NSData. How to return it to zip? I will then need to unpack it and put the contents of the file into the project itself. There should be a database that I will store locally.
I've seen snippets of Obj-C code in answers like this , but didn't quite understand how to use it for me.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2015-02-05
@Leshich

SSZipArchive library , you need to import libz.dylib in addition to the library itself

import Foundation

class DownloadManager: NSObject, NSURLSessionDownloadDelegate {
    
    let backgroundSession: NSURLSession!
    
    override init() {
        super.init()
        let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("YOUR.COM.NAME") // Вообще любой уникальный идентификатор, e.g. com.toster.zip.BackgroundSession
        backgroundSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
    }
    
    func downloadWithURL(url: NSURL) {
        let task = backgroundSession.downloadTaskWithURL(url)
        task.resume()
    }
    
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
        let destination = NSHomeDirectory().stringByAppendingString("/Desktop/unzip/")
        SSZipArchive.unzipFileAtPath(location.path!, toDestination: destination)
    }
    
}

let manager = DownloadManager()
manager.downloadWithURL(NSURL(string: "http://.../data.zip")!)

The unpacked archive will be in /Desktop/unzip/

C
Cassar, 2015-01-30
@Cassar

https://github.com/kolpanic/ZipKit

L
Leshich, 2015-02-03
@Leshich

Good. I have already found a million such libraries, but I don’t quite understand how to include and call them in the Swift code. I tied it like using a .h file, but I don’t understand further.
Tied it up

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question