Unzipping files with SSZipArchive - Swift

2019-04-10 20:39发布

问题:

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

回答1:

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)
    


回答2:

TLDR; check that your source and destination paths don't begin with the file:// prefix.

More Detail...

I had the same issue, SSZipArchive would run, the success variable would print out false, no delegates were called and no debug messages printed from SSZipArchive.

I was running it like this:

let sourcePath = sourceURL.absoluteString
let destPath = destURL.absoluteString

print("Source: \(sourcePath)")
print("Dest: \(destPath)")

let success = SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destPath, delegate: self)
print("ZipArchive - Success: \(success)")

and my log statements were printing out

Source: file:///var/containers/Bundle/Application/longAppIDHere/testApp.app/Bundle1.zip
Dest: file:///var/mobile/Containers/Data/Application/longAppIDHere/Library/Application Support/testFolder/
ZipArchive - Success: false

I changed my path constructors to:

let sourcePath = sourceURL.relativePath
let destPath = destURL.relativePath

and now SSZipArchive works fine.