Commit d0fc8183 authored by Roy Marmelstein's avatar Roy Marmelstein

Unzip function clean up

parent 3825c68f
......@@ -98,7 +98,6 @@
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>"; };
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 */
/* Begin PBXFrameworksBuildPhase section */
......@@ -192,7 +191,6 @@
children = (
3430F6391C45C8AD007473A6 /* aes */,
3430F6211C45C83F007473A6 /* minizip */,
347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */,
347E3A771C1DFFB500A11FD3 /* Zip.h */,
347E3A791C1DFFB500A11FD3 /* Info.plist */,
347E3AD71C1E04C900A11FD3 /* Zip.swift */,
......
......@@ -8,16 +8,15 @@
import Foundation
import minizip
import Darwin
public enum ZipError: ErrorType {
case FileNotFound
case UnzipError
case FileError
public var description: String {
switch self {
case .UnzipError: return NSLocalizedString("Failed to open zip file.", comment: "")
case .FileError: return NSLocalizedString("File error.", comment: "")
case .FileNotFound: return NSLocalizedString("File not found.", comment: "")
case .UnzipError: return NSLocalizedString("Failed to unzip zip file.", comment: "")
}
}
}
......@@ -25,25 +24,23 @@ public enum ZipError: ErrorType {
public class Zip {
public init () {
}
public init () {}
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)
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
if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.UnzipError
}
var ret: Int32 = 0
var crc_ret: Int32 = 0
let bufferSize = 4096
var buffer = Array<CUnsignedChar>(count: bufferSize, repeatedValue: 0)
let fileManager = NSFileManager.defaultManager()
let bufferSize: UInt32 = 4096
var buffer = Array<CUnsignedChar>(count: Int(bufferSize), repeatedValue: 0)
repeat {
ret = unzOpenCurrentFile(zip)
if ret != UNZ_OK {
......@@ -51,14 +48,11 @@ public class Zip {
}
var fileInfo = unz_file_info()
memset(&fileInfo, 0, sizeof(unz_file_info))
ret = unzGetCurrentFileInfo(zip, &fileInfo, nil, 0, nil, 0, nil, 0)
if ret != UNZ_OK {
unzCloseCurrentFile(zip)
throw ZipError.UnzipError
}
currentPosition = currentPosition + Double(fileInfo.compressed_size)
let fileNameSize = Int(fileInfo.size_filename) + 1
let fileName = UnsafeMutablePointer<CChar>.alloc(fileNameSize)
if fileName == nil {
......@@ -79,9 +73,8 @@ public class Zip {
strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/")
}
let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String)
// TODO: GET DOS DATE FROM FILEINFO
let modDate = NSDate()
let directoryAttributes = [NSFileCreationDate: modDate, NSFileModificationDate: modDate]
let creationDate = NSDate()
let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate]
if isDirectory {
try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes)
}
......@@ -93,11 +86,10 @@ public class Zip {
unzCloseCurrentFile(zip)
ret = unzGoToNextFile(zip)
}
var filePointer: UnsafeMutablePointer<FILE>
filePointer = fopen(fullPath, "wb")
while filePointer != nil {
let readBytes = unzReadCurrentFile(zip, &buffer, 4096)
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
if readBytes > 0 {
fwrite(buffer, Int(readBytes), 1, filePointer)
}
......@@ -105,22 +97,13 @@ public class Zip {
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)
crc_ret = unzCloseCurrentFile(zip)
if crc_ret == UNZ_CRCERROR {
throw ZipError.UnzipError
}
ret = unzGoToNextFile(zip)
} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE)
}
}
\ No newline at end of file
......@@ -16,8 +16,7 @@ class ViewController: UIViewController {
do {
let destinationPath = tempUnzipPath()!
let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip")
let fileManager = NSFileManager.defaultManager()
let fileExists = fileManager.fileExistsAtPath(fileAbsoluteUrl!)
print(destinationPath)
try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true)
}
catch {
......@@ -31,27 +30,6 @@ class ViewController: UIViewController {
// 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? {
var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
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