Commit 1081f87f authored by Roy Marmelstein's avatar Roy Marmelstein

Clean up

parent c044cb05
...@@ -37,7 +37,7 @@ extension Zip { ...@@ -37,7 +37,7 @@ extension Zip {
*/ */
public func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL { public func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL {
guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else { guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
let directoryName = fileName.stringByReplacingOccurrencesOfString(fileExtension, withString: "") let directoryName = fileName.stringByReplacingOccurrencesOfString(fileExtension, withString: "")
let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true) let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true)
......
...@@ -12,17 +12,15 @@ import minizip ...@@ -12,17 +12,15 @@ import minizip
/// Zip error type /// Zip error type
public enum ZipError: ErrorType { public enum ZipError: ErrorType {
case FileNotFound // File not found case FileNotFound // File not found
case UnzipError // Unzip error case UnzipFail // Unzip error
case ZipError // Unzip error case ZipFail // Zip error
case NotAZipFileError // Unzip error
/// Description variable /// Description variable
public var description: String { public var description: String {
switch self { switch self {
case .FileNotFound: return NSLocalizedString("File not found.", comment: "") case .FileNotFound: return NSLocalizedString("File not found.", comment: "")
case .UnzipError: return NSLocalizedString("Failed to unzip zip file.", comment: "") case .UnzipFail: return NSLocalizedString("Failed to unzip zip file.", comment: "")
case .ZipError: return NSLocalizedString("Failed to zip file.", comment: "") case .ZipFail: return NSLocalizedString("Failed to zip file.", comment: "")
case .NotAZipFileError: return NSLocalizedString("The file path does not contain a zip file.", comment: "")
} }
} }
} }
...@@ -57,9 +55,9 @@ public class Zip { ...@@ -57,9 +55,9 @@ public class Zip {
- throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable. - throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable.
*/ */
public func unzipFile(path: NSURL, destination: NSURL, overwrite: Bool, password: String?, progress: ((progress: Double) -> ())?) throws { public func unzipFile(path: NSURL, destination: NSURL, overwrite: Bool, password: String?, progress: ((progress: Double) -> ())?) throws {
// Check file exists at path. // Check whether a zip file exists at path.
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(path.absoluteString) == false { if fileManager.fileExistsAtPath(path.absoluteString) == false || path.pathExtension != ".zip" {
throw ZipError.FileNotFound throw ZipError.FileNotFound
} }
// Unzip set up // Unzip set up
...@@ -71,10 +69,10 @@ public class Zip { ...@@ -71,10 +69,10 @@ public class Zip {
let zip = unzOpen64(path.absoluteString) let zip = unzOpen64(path.absoluteString)
let fileAttributes = try fileManager.attributesOfItemAtPath(path.absoluteString) let fileAttributes = try fileManager.attributesOfItemAtPath(path.absoluteString)
let fileSize = fileAttributes[NSFileSize] as? Double let totalSize = fileAttributes[NSFileSize] as? Double
var currentPosition: Double = 0.0 var currentPosition: Double = 0.0
if unzGoToFirstFile(zip) != UNZ_OK { if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
repeat { repeat {
if let cPassword = password?.cStringUsingEncoding(NSASCIIStringEncoding) { if let cPassword = password?.cStringUsingEncoding(NSASCIIStringEncoding) {
...@@ -84,25 +82,25 @@ public class Zip { ...@@ -84,25 +82,25 @@ public class Zip {
ret = unzOpenCurrentFile(zip); ret = unzOpenCurrentFile(zip);
} }
if ret != UNZ_OK { if ret != UNZ_OK {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
var fileInfo = unz_file_info64() var fileInfo = unz_file_info64()
memset(&fileInfo, 0, sizeof(unz_file_info)) memset(&fileInfo, 0, sizeof(unz_file_info))
ret = unzGetCurrentFileInfo64(zip, &fileInfo, nil, 0, nil, 0, nil, 0) ret = unzGetCurrentFileInfo64(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.UnzipFail
} }
currentPosition += Double(fileInfo.compressed_size) 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 {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
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
guard var pathString = String(CString: fileName, encoding: NSUTF8StringEncoding) else { guard var pathString = String(CString: fileName, encoding: NSUTF8StringEncoding) else {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
var isDirectory = false var isDirectory = false
let fileInfoSizeFileName = Int(fileInfo.size_filename-1) let fileInfoSizeFileName = Int(fileInfo.size_filename-1)
...@@ -114,7 +112,7 @@ public class Zip { ...@@ -114,7 +112,7 @@ public class Zip {
pathString = pathString.stringByReplacingOccurrencesOfString("\\", withString: "/") pathString = pathString.stringByReplacingOccurrencesOfString("\\", withString: "/")
} }
guard let fullPath = destination.URLByAppendingPathComponent(pathString).path else { guard let fullPath = destination.URLByAppendingPathComponent(pathString).path else {
throw ZipError.UnzipError throw ZipError.UnzipFail
} }
let creationDate = NSDate() let creationDate = NSDate()
let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate] let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate]
...@@ -144,10 +142,10 @@ public class Zip { ...@@ -144,10 +142,10 @@ public class Zip {
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.UnzipFail
} }
if let progressHandler = progress, let fileSize = fileSize{ if let progressHandler = progress, let totalSize = totalSize{
progressHandler(progress: (currentPosition/fileSize)) progressHandler(progress: (currentPosition/totalSize))
} }
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)
...@@ -174,24 +172,25 @@ public class Zip { ...@@ -174,24 +172,25 @@ public class Zip {
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
var currentPosition: Double = 0.0 var currentPosition: Double = 0.0
var totalSize: Double = 0.0 var totalSize: Double = 0.0
// If progress handler exists, get total fileSize // Check if paths exist and get totalSize for progress handler
if progress != nil { for path in paths {
for path in paths { if fileManager.fileExistsAtPath(path.absoluteString) == false {
do { throw ZipError.FileNotFound
let fileAttributes = try fileManager.attributesOfItemAtPath(path.path!) }
let fileSize = fileAttributes[NSFileSize] as? Double do {
if let fileSize = fileSize { let fileAttributes = try fileManager.attributesOfItemAtPath(path.path!)
totalSize += fileSize let fileSize = fileAttributes[NSFileSize] as? Double
} if let fileSize = fileSize {
totalSize += fileSize
} }
catch {}
} }
catch {}
} }
let zip = zipOpen(destination.path!, APPEND_STATUS_CREATE) let zip = zipOpen(destination.path!, APPEND_STATUS_CREATE)
for path in paths { for path in paths {
let input = fopen(path.path!, "r") let input = fopen(path.path!, "r")
if input == nil { if input == nil {
throw ZipError.ZipError throw ZipError.ZipFail
} }
let fileName = path.lastPathComponent let fileName = path.lastPathComponent
var zipInfo: zip_fileinfo = zip_fileinfo(tmz_date: tm_zip(tm_sec: 0, tm_min: 0, tm_hour: 0, tm_mday: 0, tm_mon: 0, tm_year: 0), dosDate: 0, internal_fa: 0, external_fa: 0) var zipInfo: zip_fileinfo = zip_fileinfo(tmz_date: tm_zip(tm_sec: 0, tm_min: 0, tm_hour: 0, tm_mday: 0, tm_mon: 0, tm_year: 0), dosDate: 0, internal_fa: 0, external_fa: 0)
...@@ -219,12 +218,12 @@ public class Zip { ...@@ -219,12 +218,12 @@ public class Zip {
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0) zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0)
} }
else { else {
throw ZipError.ZipError throw ZipError.ZipFail
} }
var len: Int = 0 var length: Int = 0
while (feof(input) == 0) { while (feof(input) == 0) {
len = fread(buffer, 1, chunkSize, input) length = fread(buffer, 1, chunkSize, input)
zipWriteInFileInZip(zip, buffer, UInt32(len)) zipWriteInFileInZip(zip, buffer, UInt32(length))
} }
if let progressHandler = progress{ if let progressHandler = progress{
progressHandler(progress: (currentPosition/totalSize)) progressHandler(progress: (currentPosition/totalSize))
......
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