Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzipping files with SSZipArchive - Swift

I'm trying to unzip a file using the SSZipArchive framework.

let unzipper = SSZipArchive.unzipFileAtPath(String(document), toDestination: String(documentsUrl))

This is what I'm trying at the moment to unzip the file, and here is the path of the file:

unzipFileAtPath - Document at path: file:///private/var/mobile/Containers/Data/Application/94ADDB12-78A2-4798-856D-0626C41B7AC2/Documents/tSOUTrIayb.zip false

And I am trying to unzip it to this path:

NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

Unfortunately it doesn't seem to be working, each time there is nothing new being saved into the documents directory which I am printing out. I also print the 'unzipper' variable which just prints false, whatever that means.

I can't find any documentation for the framework so I'm not entirely sure how to get this working

like image 288
jackchmbrln Avatar asked Jan 07 '23 04:01

jackchmbrln


1 Answers

Assume that you have implement all things, then also providing solution in few steps

  1. Drag SSZipArchive folder from the demo which you can download from here.

  2. Write #import "SSZipArchive.h” in yourProjectname-Bridging-Header.h if you already have. Otherwise create new header file named as mention above.

  3. If you want to use delegate methods set delegate to class

    class YourViewController: UIViewController, SSZipArchiveDelegate {.......
    
  4. For creating zip form folder I have added sample folder to project (Main bundle)

  5. First create folder in document directory where you want to save your zip file

    var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDir = paths[0]
    let zipPath =  documentsDir.stringByAppendingString("/MyZipFiles") // My folder name in document directory
    
    let fileManager = NSFileManager.defaultManager()
    
    let success = fileManager.fileExistsAtPath(zipPath) as Bool
    
    if success == false {
    
        do {
    
            try! fileManager.createDirectoryAtPath(zipPath, withIntermediateDirectories: true, attributes: nil)
        }
    }
    
  6. Now create zip file

    var inputPath = NSBundle.mainBundle().resourcePath
    inputPath = inputPath!.stringByAppendingString("/Sample") // My folder which already put into project
    
    let archivePath = zipPath.stringByAppendingString(“/Demo.zip") // Sample folder is going to zip with name Demo.zip
    SSZipArchive.createZipFileAtPath(archivePath, withContentsOfDirectory:inputPath)
    
  7. For unzipping file create folder in document directory (Where you want to save)

    let destPath = zipPath.stringByAppendingString("/Hello")
    let fileManager = NSFileManager.defaultManager()
    
    let success = fileManager.fileExistsAtPath(destPath) as Bool
    
    if success == false {
    
        do {
    
            try! fileManager.createDirectoryAtPath(destPath, withIntermediateDirectories: true, attributes: nil)
        }
    }
    
  8. Unzip folder

    SSZipArchive.unzipFileAtPath(archivePath, toDestination:destPath, delegate:self)
    
  9. Print your path, and you can check there your file will be saved

    print(zipPath)
    
like image 101
VRAwesome Avatar answered Jan 15 '23 10:01

VRAwesome