From 79dfb476d50a77a7a007de5bd3492447d99f68b3 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Thu, 14 Jan 2016 00:08:43 +0100 Subject: [PATCH] Cleaner implementaiton of unzip --- Zip/Zip.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 4c45258..2b609e3 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -66,25 +66,28 @@ 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 + guard var pathString = String(CString: fileName, encoding: NSUTF8StringEncoding) else { + throw ZipError.UnzipError + } var isDirectory = false let fileInfoSizeFileName = Int(fileInfo.size_filename-1) if (fileName[fileInfoSizeFileName] == "/".cStringUsingEncoding(NSUTF8StringEncoding)!.first! || fileName[fileInfoSizeFileName] == "\\".cStringUsingEncoding(NSUTF8StringEncoding)!.first!) { isDirectory = true; } free(fileName) - - if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) { - strPath = strPath.stringByReplacingOccurrencesOfString("\\", withString: "/") + if pathString.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")) != nil { + pathString = pathString.stringByReplacingOccurrencesOfString("\\", withString: "/") + } + guard let fullPath = destination.URLByAppendingPathComponent(pathString).path else { + throw ZipError.UnzipError } - let fullPath = destination.URLByAppendingPathComponent(strPath as String).path! let creationDate = NSDate() let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate] if isDirectory { try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes) } else { - try fileManager.createDirectoryAtPath((fullPath as NSString).stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: directoryAttributes) + try fileManager.createDirectoryAtPath(destination.path!, withIntermediateDirectories: true, attributes: directoryAttributes) } if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite { -- 2.26.2