Commit 3825c68f authored by Roy Marmelstein's avatar Roy Marmelstein

Successful unzipping buffer

parent 3b5bfe33
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
3430F5D61C45B7FB007473A6 /* ZipHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3430F5D51C45B7FB007473A6 /* ZipHelpers.swift */; };
3430F6201C45C805007473A6 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 3430F61F1C45C805007473A6 /* libz.tbd */; }; 3430F6201C45C805007473A6 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 3430F61F1C45C805007473A6 /* libz.tbd */; };
3430F62D1C45C851007473A6 /* crypt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6221C45C851007473A6 /* crypt.h */; }; 3430F62D1C45C851007473A6 /* crypt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6221C45C851007473A6 /* crypt.h */; };
3430F62E1C45C851007473A6 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6231C45C851007473A6 /* include.h */; }; 3430F62E1C45C851007473A6 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6231C45C851007473A6 /* include.h */; };
...@@ -58,7 +57,6 @@ ...@@ -58,7 +57,6 @@
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
3430F5D51C45B7FB007473A6 /* ZipHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipHelpers.swift; sourceTree = "<group>"; };
3430F61F1C45C805007473A6 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; 3430F61F1C45C805007473A6 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
3430F6221C45C851007473A6 /* crypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crypt.h; sourceTree = "<group>"; }; 3430F6221C45C851007473A6 /* crypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crypt.h; sourceTree = "<group>"; };
3430F6231C45C851007473A6 /* include.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = include.h; sourceTree = "<group>"; }; 3430F6231C45C851007473A6 /* include.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = include.h; sourceTree = "<group>"; };
...@@ -197,7 +195,6 @@ ...@@ -197,7 +195,6 @@
347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */, 347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */,
347E3A771C1DFFB500A11FD3 /* Zip.h */, 347E3A771C1DFFB500A11FD3 /* Zip.h */,
347E3A791C1DFFB500A11FD3 /* Info.plist */, 347E3A791C1DFFB500A11FD3 /* Info.plist */,
3430F5D51C45B7FB007473A6 /* ZipHelpers.swift */,
347E3AD71C1E04C900A11FD3 /* Zip.swift */, 347E3AD71C1E04C900A11FD3 /* Zip.swift */,
); );
path = Zip; path = Zip;
...@@ -351,7 +348,6 @@ ...@@ -351,7 +348,6 @@
3430F6351C45C851007473A6 /* zip.c in Sources */, 3430F6351C45C851007473A6 /* zip.c in Sources */,
3430F6311C45C851007473A6 /* mztools.c in Sources */, 3430F6311C45C851007473A6 /* mztools.c in Sources */,
3430F6511C45C8AD007473A6 /* aescrypt.c in Sources */, 3430F6511C45C8AD007473A6 /* aescrypt.c in Sources */,
3430F5D61C45B7FB007473A6 /* ZipHelpers.swift in Sources */,
3430F6601C45C8AD007473A6 /* pwd2key.c in Sources */, 3430F6601C45C8AD007473A6 /* pwd2key.c in Sources */,
3430F65A1C45C8AD007473A6 /* fileenc.c in Sources */, 3430F65A1C45C8AD007473A6 /* fileenc.c in Sources */,
); );
......
...@@ -29,7 +29,7 @@ public class Zip { ...@@ -29,7 +29,7 @@ public class Zip {
} }
public func unzipFile(path: String, destination: String, overwrite: Bool, password: String?) throws { public func unzipFile(path: String, destination: String, overwrite: Bool) throws {
let zip = unzOpen(path) let zip = unzOpen(path)
var currentPosition = 0.0 var currentPosition = 0.0
var globalInfo: unz_global_info = unz_global_info(number_entry: 0, number_disk_with_CD: 0, size_comment: 0) var globalInfo: unz_global_info = unz_global_info(number_entry: 0, number_disk_with_CD: 0, size_comment: 0)
...@@ -44,77 +44,82 @@ public class Zip { ...@@ -44,77 +44,82 @@ public class Zip {
let bufferSize = 4096 let bufferSize = 4096
var buffer = Array<CUnsignedChar>(count: bufferSize, repeatedValue: 0) var buffer = Array<CUnsignedChar>(count: bufferSize, repeatedValue: 0)
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
if let password = password where password.characters.count > 0 { repeat {
ret = unzOpenCurrentFilePassword(zip, password.cStringUsingEncoding(NSASCIIStringEncoding)!)
}
else {
ret = unzOpenCurrentFile(zip) ret = unzOpenCurrentFile(zip)
} if ret != UNZ_OK {
if ret != UNZ_OK { throw ZipError.UnzipError
throw ZipError.UnzipError }
} 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)
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 { throw ZipError.UnzipError
throw ZipError.UnzipError }
} unzGetCurrentFileInfo(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0)
unzGetCurrentFileInfo(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
var strPath = String.fromCString(fileName)! as NSString 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 (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) {
if (strPath.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "/\\")).location != NSNotFound) { 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
// TODO: GET DOS DATE FROM FILEINFO let modDate = NSDate()
let modDate = NSDate() let directoryAttributes = [NSFileCreationDate: modDate, NSFileModificationDate: modDate]
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) }
} else {
else { try fileManager.createDirectoryAtPath((fullPath as NSString).stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: directoryAttributes)
try fileManager.createDirectoryAtPath((fullPath as NSString).stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: directoryAttributes) }
}
if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite {
if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite { unzCloseCurrentFile(zip)
unzCloseCurrentFile(zip) ret = unzGoToNextFile(zip)
}
var filePointer: UnsafeMutablePointer<FILE>
filePointer = fopen(fullPath, "wb")
while filePointer != nil {
let readBytes = unzReadCurrentFile(zip, &buffer, 4096)
if readBytes > 0 {
fwrite(buffer, Int(readBytes), 1, filePointer)
}
else {
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) ret = unzGoToNextFile(zip)
}
} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE)
var filePointer: UnsafeMutablePointer<FILE>
filePointer = fopen(fullPath, "wb")
let readBytes = unzReadCurrentFile(zip, &buffer, 4096)
fwrite(buffer, Int(readBytes), 1, filePointer)
if filePointer != nil {
if ((fullPath as NSString).pathExtension.lowercaseString == "zip") {
// nested zip
try unzipFile(fullPath, destination: (fullPath as NSString).stringByDeletingLastPathComponent, overwrite: overwrite, password: password)
}
}
fclose(filePointer)
crc_ret = unzCloseCurrentFile(zip)
if crc_ret == UNZ_CRCERROR {
throw ZipError.UnzipError
}
ret = unzGoToNextFile(zip)
currentPosition++
} }
......
//
// ZipHelpers.swift
// Zip
//
// Created by Roy Marmelstein on 12/01/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import Foundation
public enum FopenMode: String {
case Read = "r"
case Write = "w"
}
public func ZIPFopen(path: String..., mode: FopenMode = .Read) throws -> UnsafeMutablePointer<FILE> {
let path = joinPathComponents(path)
let f = fopen(path, mode.rawValue)
guard f != nil else { throw ZipError.FileError }
return f
}
/**
Joins path components, unless a component is an absolute
path, in which case it discards all previous path components.
*/
func joinPathComponents(join: [String]) -> String {
guard join.count > 0 else { return "" }
return join.dropFirst(1).reduce(join[0]) {
if $1.hasPrefix("/") {
return $1
} else {
return $0 + "/" + $1
}
}
}
\ No newline at end of file
...@@ -18,7 +18,7 @@ class ViewController: UIViewController { ...@@ -18,7 +18,7 @@ class ViewController: UIViewController {
let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip") let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip")
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
let fileExists = fileManager.fileExistsAtPath(fileAbsoluteUrl!) let fileExists = fileManager.fileExistsAtPath(fileAbsoluteUrl!)
try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true, password: nil) try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true)
} }
catch { catch {
print("oops") print("oops")
......
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