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
Assume that you have implement all things, then also providing solution in few steps
Drag SSZipArchive
folder from the demo which you can download from here.
Write #import "SSZipArchive.h”
in yourProjectname-Bridging-Header.h
if you already have. Otherwise create new header file named as mention above.
If you want to use delegate methods set delegate to class
class YourViewController: UIViewController, SSZipArchiveDelegate {.......
For creating zip form folder I have added sample folder to project (Main bundle)
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)
}
}
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)
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)
}
}
Unzip folder
SSZipArchive.unzipFileAtPath(archivePath, toDestination:destPath, delegate:self)
Print your path, and you can check there your file will be saved
print(zipPath)