Commit 639a0727 authored by Roy Marmelstein's avatar Roy Marmelstein

Destination as NSURL

parent b48db457
...@@ -26,21 +26,22 @@ public class Zip { ...@@ -26,21 +26,22 @@ public class Zip {
public init () {} 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. // Check file exists at path.
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(path.absoluteString) == false { if fileManager.fileExistsAtPath(path.absoluteString) == false {
throw ZipError.FileNotFound throw ZipError.FileNotFound
} }
let zip = unzOpen64(path.absoluteString) // Unzip set up
// Begin unzipping
if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.UnzipError
}
var ret: Int32 = 0 var ret: Int32 = 0
var crc_ret: Int32 = 0 var crc_ret: Int32 = 0
let bufferSize: UInt32 = 4096 let bufferSize: UInt32 = 4096
var buffer = Array<CUnsignedChar>(count: Int(bufferSize), repeatedValue: 0) var buffer = Array<CUnsignedChar>(count: Int(bufferSize), repeatedValue: 0)
// Begin unzipping
let zip = unzOpen64(path.absoluteString)
if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.UnzipError
}
repeat { repeat {
if let cPassword = password?.cStringUsingEncoding(NSASCIIStringEncoding) { if let cPassword = password?.cStringUsingEncoding(NSASCIIStringEncoding) {
ret = unzOpenCurrentFilePassword(zip, cPassword) ret = unzOpenCurrentFilePassword(zip, cPassword)
...@@ -65,7 +66,6 @@ public class Zip { ...@@ -65,7 +66,6 @@ public class Zip {
} }
unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0)
fileName[Int(fileInfo.size_filename)] = 0 fileName[Int(fileInfo.size_filename)] = 0
var strPath = String.fromCString(fileName)! as NSString var strPath = String.fromCString(fileName)! as NSString
var isDirectory = false var isDirectory = false
let fileInfoSizeFileName = Int(fileInfo.size_filename-1) let fileInfoSizeFileName = Int(fileInfo.size_filename-1)
...@@ -77,7 +77,7 @@ public class Zip { ...@@ -77,7 +77,7 @@ public class Zip {
if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) { if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) {
strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/") 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 creationDate = NSDate()
let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate] let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate]
if isDirectory { if isDirectory {
......
...@@ -31,7 +31,7 @@ class ViewController: UIViewController { ...@@ -31,7 +31,7 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated. // Dispose of any resources that can be recreated.
} }
func tempUnzipPath() -> String? { func tempUnzipPath() -> NSURL? {
var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
path += "/\(NSUUID().UUIDString)" path += "/\(NSUUID().UUIDString)"
let url = NSURL(fileURLWithPath: path) let url = NSURL(fileURLWithPath: path)
...@@ -41,12 +41,7 @@ class ViewController: UIViewController { ...@@ -41,12 +41,7 @@ class ViewController: UIViewController {
} catch { } catch {
return nil return nil
} }
return url
if let path = url.path {
return path
}
return nil
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment