Commit 79dfb476 authored by Roy Marmelstein's avatar Roy Marmelstein

Cleaner implementaiton of unzip

parent 639a0727
...@@ -66,25 +66,28 @@ public class Zip { ...@@ -66,25 +66,28 @@ 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 guard var pathString = String(CString: fileName, encoding: NSUTF8StringEncoding) else {
throw ZipError.UnzipError
}
var isDirectory = false var isDirectory = false
let fileInfoSizeFileName = Int(fileInfo.size_filename-1) let fileInfoSizeFileName = Int(fileInfo.size_filename-1)
if (fileName[fileInfoSizeFileName] == "/".cStringUsingEncoding(NSUTF8StringEncoding)!.first! || fileName[fileInfoSizeFileName] == "\\".cStringUsingEncoding(NSUTF8StringEncoding)!.first!) { if (fileName[fileInfoSizeFileName] == "/".cStringUsingEncoding(NSUTF8StringEncoding)!.first! || fileName[fileInfoSizeFileName] == "\\".cStringUsingEncoding(NSUTF8StringEncoding)!.first!) {
isDirectory = true; isDirectory = true;
} }
free(fileName) free(fileName)
if pathString.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")) != nil {
if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) { pathString = pathString.stringByReplacingOccurrencesOfString("\\", withString: "/")
strPath = strPath.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 creationDate = NSDate()
let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate] let directoryAttributes = [NSFileCreationDate: creationDate, NSFileModificationDate: creationDate]
if isDirectory { if isDirectory {
try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes) try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes)
} }
else { 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 { if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite {
......
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