From 3825c68fc28738759a14c852833cabd3452df7b7 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Wed, 13 Jan 2016 02:13:36 +0100 Subject: [PATCH] Successful unzipping buffer --- Zip.xcodeproj/project.pbxproj | 4 - Zip/Zip.swift | 143 ++++++++++---------- Zip/ZipHelpers.swift | 37 ----- examples/Sample/Sample/ViewController.swift | 2 +- 4 files changed, 75 insertions(+), 111 deletions(-) delete mode 100644 Zip/ZipHelpers.swift diff --git a/Zip.xcodeproj/project.pbxproj b/Zip.xcodeproj/project.pbxproj index a981dcc..1d17e39 100644 --- a/Zip.xcodeproj/project.pbxproj +++ b/Zip.xcodeproj/project.pbxproj @@ -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 = ""; }; 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 = ""; }; 3430F6231C45C851007473A6 /* include.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = include.h; sourceTree = ""; }; @@ -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 */, ); diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 5a4d08f..306f2fb 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -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,77 +44,82 @@ public class Zip { let bufferSize = 4096 var buffer = Array(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 - } - var fileInfo = unz_file_info() - memset(&fileInfo, 0, sizeof(unz_file_info)) - - ret = unzGetCurrentFileInfo(zip, &fileInfo, nil, 0, nil, 0, nil, 0) - if ret != UNZ_OK { - unzCloseCurrentFile(zip) - throw ZipError.UnzipError - } - - currentPosition = currentPosition + Double(fileInfo.compressed_size) - let fileNameSize = Int(fileInfo.size_filename) + 1 - let fileName = UnsafeMutablePointer.alloc(fileNameSize) - if fileName == nil { - throw ZipError.UnzipError - } - unzGetCurrentFileInfo(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) - fileName[Int(fileInfo.size_filename)] = 0 - - var strPath = String.fromCString(fileName)! as NSString - 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: "/") - } - let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String) - // TODO: GET DOS DATE FROM FILEINFO - let modDate = NSDate() - let directoryAttributes = [NSFileCreationDate: modDate, NSFileModificationDate: modDate] - if isDirectory { - try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes) - } - else { - try fileManager.createDirectoryAtPath((fullPath as NSString).stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: directoryAttributes) - } - - if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite { - unzCloseCurrentFile(zip) + if ret != UNZ_OK { + throw ZipError.UnzipError + } + var fileInfo = unz_file_info() + memset(&fileInfo, 0, sizeof(unz_file_info)) + + ret = unzGetCurrentFileInfo(zip, &fileInfo, nil, 0, nil, 0, nil, 0) + if ret != UNZ_OK { + unzCloseCurrentFile(zip) + throw ZipError.UnzipError + } + + currentPosition = currentPosition + Double(fileInfo.compressed_size) + let fileNameSize = Int(fileInfo.size_filename) + 1 + let fileName = UnsafeMutablePointer.alloc(fileNameSize) + if fileName == nil { + throw ZipError.UnzipError + } + unzGetCurrentFileInfo(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) + fileName[Int(fileInfo.size_filename)] = 0 + + var strPath = String.fromCString(fileName)! as NSString + 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: "/") + } + let fullPath = (destination as NSString).stringByAppendingPathComponent(strPath as String) + // TODO: GET DOS DATE FROM FILEINFO + let modDate = NSDate() + let directoryAttributes = [NSFileCreationDate: modDate, NSFileModificationDate: modDate] + if isDirectory { + try fileManager.createDirectoryAtPath(fullPath, withIntermediateDirectories: true, attributes: directoryAttributes) + } + else { + try fileManager.createDirectoryAtPath((fullPath as NSString).stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: directoryAttributes) + } + + if fileManager.fileExistsAtPath(fullPath) && !isDirectory && !overwrite { + unzCloseCurrentFile(zip) + ret = unzGoToNextFile(zip) + } + + var filePointer: UnsafeMutablePointer + 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) - } + + } while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE) - var filePointer: UnsafeMutablePointer - 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++ } diff --git a/Zip/ZipHelpers.swift b/Zip/ZipHelpers.swift deleted file mode 100644 index a1fb528..0000000 --- a/Zip/ZipHelpers.swift +++ /dev/null @@ -1,37 +0,0 @@ -// -// 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 { - 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 diff --git a/examples/Sample/Sample/ViewController.swift b/examples/Sample/Sample/ViewController.swift index 471374e..a7a29a8 100644 --- a/examples/Sample/Sample/ViewController.swift +++ b/examples/Sample/Sample/ViewController.swift @@ -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") -- 2.26.2