Commit d0fc8183 authored by Roy Marmelstein's avatar Roy Marmelstein

Unzip function clean up

parent 3825c68f
...@@ -98,7 +98,6 @@ ...@@ -98,7 +98,6 @@
347E3A831C1DFFB500A11FD3 /* ZipTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipTests.swift; sourceTree = "<group>"; }; 347E3A831C1DFFB500A11FD3 /* ZipTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipTests.swift; sourceTree = "<group>"; };
347E3A851C1DFFB500A11FD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 347E3A851C1DFFB500A11FD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
347E3AD71C1E04C900A11FD3 /* Zip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Zip.swift; sourceTree = "<group>"; }; 347E3AD71C1E04C900A11FD3 /* Zip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Zip.swift; sourceTree = "<group>"; };
347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.1.2.5.tbd; path = usr/lib/libz.1.2.5.tbd; sourceTree = SDKROOT; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -192,7 +191,6 @@ ...@@ -192,7 +191,6 @@
children = ( children = (
3430F6391C45C8AD007473A6 /* aes */, 3430F6391C45C8AD007473A6 /* aes */,
3430F6211C45C83F007473A6 /* minizip */, 3430F6211C45C83F007473A6 /* minizip */,
347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */,
347E3A771C1DFFB500A11FD3 /* Zip.h */, 347E3A771C1DFFB500A11FD3 /* Zip.h */,
347E3A791C1DFFB500A11FD3 /* Info.plist */, 347E3A791C1DFFB500A11FD3 /* Info.plist */,
347E3AD71C1E04C900A11FD3 /* Zip.swift */, 347E3AD71C1E04C900A11FD3 /* Zip.swift */,
......
...@@ -8,16 +8,15 @@ ...@@ -8,16 +8,15 @@
import Foundation import Foundation
import minizip import minizip
import Darwin
public enum ZipError: ErrorType { public enum ZipError: ErrorType {
case FileNotFound
case UnzipError case UnzipError
case FileError
public var description: String { public var description: String {
switch self { switch self {
case .UnzipError: return NSLocalizedString("Failed to open zip file.", comment: "") case .FileNotFound: return NSLocalizedString("File not found.", comment: "")
case .FileError: return NSLocalizedString("File error.", comment: "") case .UnzipError: return NSLocalizedString("Failed to unzip zip file.", comment: "")
} }
} }
} }
...@@ -25,25 +24,23 @@ public enum ZipError: ErrorType { ...@@ -25,25 +24,23 @@ public enum ZipError: ErrorType {
public class Zip { public class Zip {
public init () { public init () {}
}
public func unzipFile(path: String, destination: String, overwrite: Bool) throws { public func unzipFile(path: String, destination: String, overwrite: Bool) throws {
// Check file exists at path.
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(path) == false {
throw ZipError.FileNotFound
}
let zip = unzOpen(path) let zip = unzOpen(path)
var currentPosition = 0.0
var globalInfo: unz_global_info = unz_global_info(number_entry: 0, number_disk_with_CD: 0, size_comment: 0)
unzGetGlobalInfo(zip, &globalInfo)
// Begin unzipping // Begin unzipping
if unzGoToFirstFile(zip) != UNZ_OK { if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.UnzipError 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 = 4096 var buffer = Array<CUnsignedChar>(count: Int(bufferSize), repeatedValue: 0)
var buffer = Array<CUnsignedChar>(count: bufferSize, repeatedValue: 0)
let fileManager = NSFileManager.defaultManager()
repeat { repeat {
ret = unzOpenCurrentFile(zip) ret = unzOpenCurrentFile(zip)
if ret != UNZ_OK { if ret != UNZ_OK {
...@@ -51,14 +48,11 @@ public class Zip { ...@@ -51,14 +48,11 @@ public class Zip {
} }
var fileInfo = unz_file_info() var fileInfo = unz_file_info()
memset(&fileInfo, 0, sizeof(unz_file_info)) memset(&fileInfo, 0, sizeof(unz_file_info))
ret = unzGetCurrentFileInfo(zip, &fileInfo, nil, 0, nil, 0, nil, 0) ret = unzGetCurrentFileInfo(zip, &fileInfo, nil, 0, nil, 0, nil, 0)
if ret != UNZ_OK { if ret != UNZ_OK {
unzCloseCurrentFile(zip) unzCloseCurrentFile(zip)
throw ZipError.UnzipError throw ZipError.UnzipError
} }
currentPosition = currentPosition + Double(fileInfo.compressed_size)
let fileNameSize = Int(fileInfo.size_filename) + 1 let fileNameSize = Int(fileInfo.size_filename) + 1
let fileName = UnsafeMutablePointer<CChar>.alloc(fileNameSize) let fileName = UnsafeMutablePointer<CChar>.alloc(fileNameSize)
if fileName == nil { if fileName == nil {
...@@ -79,9 +73,8 @@ public class Zip { ...@@ -79,9 +73,8 @@ public class Zip {
strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/") strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/")
} }
let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String) let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String)
// TODO: GET DOS DATE FROM FILEINFO let creationDate = NSDate()
let modDate = NSDate() let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate]
let directoryAttributes = [NSFileCreationDate: modDate, NSFileModificationDate: modDate]
if isDirectory { if isDirectory {
try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes) try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes)
} }
...@@ -93,11 +86,10 @@ public class Zip { ...@@ -93,11 +86,10 @@ public class Zip {
unzCloseCurrentFile(zip) unzCloseCurrentFile(zip)
ret = unzGoToNextFile(zip) ret = unzGoToNextFile(zip)
} }
var filePointer: UnsafeMutablePointer<FILE> var filePointer: UnsafeMutablePointer<FILE>
filePointer = fopen(fullPath, "wb") filePointer = fopen(fullPath, "wb")
while filePointer != nil { while filePointer != nil {
let readBytes = unzReadCurrentFile(zip, &buffer, 4096) let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
if readBytes > 0 { if readBytes > 0 {
fwrite(buffer, Int(readBytes), 1, filePointer) fwrite(buffer, Int(readBytes), 1, filePointer)
} }
...@@ -105,22 +97,13 @@ public class Zip { ...@@ -105,22 +97,13 @@ public class Zip {
break break
} }
} }
if filePointer != nil {
if ((fullPath as NSString).pathExtension.lowercaseString == "zip") {
// nested zip
try unzipFile(fullPath, destination: (fullPath as NSString).stringByDeletingLastPathComponent, overwrite: overwrite)
}
}
fclose(filePointer) fclose(filePointer)
crc_ret = unzCloseCurrentFile(zip) crc_ret = unzCloseCurrentFile(zip)
if crc_ret == UNZ_CRCERROR { if crc_ret == UNZ_CRCERROR {
throw ZipError.UnzipError throw ZipError.UnzipError
} }
ret = unzGoToNextFile(zip) ret = unzGoToNextFile(zip)
} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE) } while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE)
} }
} }
\ No newline at end of file
...@@ -16,8 +16,7 @@ class ViewController: UIViewController { ...@@ -16,8 +16,7 @@ class ViewController: UIViewController {
do { do {
let destinationPath = tempUnzipPath()! let destinationPath = tempUnzipPath()!
let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip") let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip")
let fileManager = NSFileManager.defaultManager() print(destinationPath)
let fileExists = fileManager.fileExistsAtPath(fileAbsoluteUrl!)
try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true) try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true)
} }
catch { catch {
...@@ -31,27 +30,6 @@ class ViewController: UIViewController { ...@@ -31,27 +30,6 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated. // Dispose of any resources that can be recreated.
} }
func tempCopyPath() -> String? {
var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
path += "master.zip"
let url = NSURL(fileURLWithPath: path)
do {
try NSFileManager.defaultManager().createDirectoryAtURL(url, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
if let path = url.path {
return path
}
return nil
}
func tempUnzipPath() -> String? { func tempUnzipPath() -> String? {
var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
path += "/\(NSUUID().UUIDString)" path += "/\(NSUUID().UUIDString)"
......
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