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

Successful unzipping buffer

parent 3b5bfe33
......@@ -7,7 +7,6 @@
objects = {
/* 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 */; };
3430F62D1C45C851007473A6 /* crypt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6221C45C851007473A6 /* crypt.h */; };
3430F62E1C45C851007473A6 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6231C45C851007473A6 /* include.h */; };
......@@ -58,7 +57,6 @@
/* End PBXContainerItemProxy 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; };
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>"; };
......@@ -197,7 +195,6 @@
347E3B1E1C1E1CB500A11FD3 /* libz.1.2.5.tbd */,
347E3A771C1DFFB500A11FD3 /* Zip.h */,
347E3A791C1DFFB500A11FD3 /* Info.plist */,
3430F5D51C45B7FB007473A6 /* ZipHelpers.swift */,
347E3AD71C1E04C900A11FD3 /* Zip.swift */,
);
path = Zip;
......@@ -351,7 +348,6 @@
3430F6351C45C851007473A6 /* zip.c in Sources */,
3430F6311C45C851007473A6 /* mztools.c in Sources */,
3430F6511C45C8AD007473A6 /* aescrypt.c in Sources */,
3430F5D61C45B7FB007473A6 /* ZipHelpers.swift in Sources */,
3430F6601C45C8AD007473A6 /* pwd2key.c in Sources */,
3430F65A1C45C8AD007473A6 /* fileenc.c in Sources */,
);
......
......@@ -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)
var currentPosition = 0.0
var globalInfo: unz_global_info = unz_global_info(number_entry: 0, number_disk_with_CD: 0, size_comment: 0)
......@@ -44,12 +44,8 @@ public class Zip {
let bufferSize = 4096
var buffer = Array<CUnsignedChar>(count: bufferSize, repeatedValue: 0)
let fileManager = NSFileManager.defaultManager()
if let password = password where password.characters.count > 0 {
ret = unzOpenCurrentFilePassword(zip, password.cStringUsingEncoding(NSASCIIStringEncoding)!)
}
else {
repeat {
ret = unzOpenCurrentFile(zip)
}
if ret != UNZ_OK {
throw ZipError.UnzipError
}
......@@ -100,12 +96,19 @@ public class 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, password: password)
try unzipFile(fullPath, destination: (fullPath as NSString).stringByDeletingLastPathComponent, overwrite: overwrite)
}
}
fclose(filePointer)
......@@ -114,7 +117,9 @@ public class Zip {
throw ZipError.UnzipError
}
ret = unzGoToNextFile(zip)
currentPosition++
} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE)
}
......
//
// 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 {
let fileAbsoluteUrl = NSBundle.mainBundle().pathForResource("master", ofType: "zip")
let fileManager = NSFileManager.defaultManager()
let fileExists = fileManager.fileExistsAtPath(fileAbsoluteUrl!)
try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true, password: nil)
try Zip().unzipFile(fileAbsoluteUrl!, destination: destinationPath, overwrite: true)
}
catch {
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