From 639a0727efcd72ea63837b54d5ce4482b3b5f085 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Wed, 13 Jan 2016 23:34:31 +0100 Subject: [PATCH] Destination as NSURL --- Zip/Zip.swift | 16 ++++++++-------- examples/Sample/Sample/ViewController.swift | 9 ++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 80d1a89..4c45258 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -26,21 +26,22 @@ public class Zip { public init () {} - public func unzipFile(path: NSURL, destination: String, overwrite: Bool, password: String?) throws { + public func unzipFile(path: NSURL, destination: NSURL, overwrite: Bool, password: String?) throws { // Check file exists at path. let fileManager = NSFileManager.defaultManager() if fileManager.fileExistsAtPath(path.absoluteString) == false { throw ZipError.FileNotFound } - let zip = unzOpen64(path.absoluteString) - // Begin unzipping - if unzGoToFirstFile(zip) != UNZ_OK { - throw ZipError.UnzipError - } + // Unzip set up var ret: Int32 = 0 var crc_ret: Int32 = 0 let bufferSize: UInt32 = 4096 var buffer = Array(count: Int(bufferSize), repeatedValue: 0) + // Begin unzipping + let zip = unzOpen64(path.absoluteString) + if unzGoToFirstFile(zip) != UNZ_OK { + throw ZipError.UnzipError + } repeat { if let cPassword = password?.cStringUsingEncoding(NSASCIIStringEncoding) { ret = unzOpenCurrentFilePassword(zip, cPassword) @@ -65,7 +66,6 @@ public class Zip { } unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) fileName[Int(fileInfo.size_filename)] = 0 - var strPath = String.fromCString(fileName)! as NSString var isDirectory = false let fileInfoSizeFileName = Int(fileInfo.size_filename-1) @@ -77,7 +77,7 @@ public class Zip { if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) { strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/") } - let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String) + let fullPath = destination.URLByAppendingPathComponent(strPath as String).path! let creationDate = NSDate() let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate] if isDirectory { diff --git a/examples/Sample/Sample/ViewController.swift b/examples/Sample/Sample/ViewController.swift index d5cd67b..1d47c02 100644 --- a/examples/Sample/Sample/ViewController.swift +++ b/examples/Sample/Sample/ViewController.swift @@ -31,7 +31,7 @@ class ViewController: UIViewController { // Dispose of any resources that can be recreated. } - func tempUnzipPath() -> String? { + func tempUnzipPath() -> NSURL? { var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] path += "/\(NSUUID().UUIDString)" let url = NSURL(fileURLWithPath: path) @@ -41,12 +41,7 @@ class ViewController: UIViewController { } catch { return nil } - - if let path = url.path { - return path - } - - return nil + return url } } -- 2.26.2