From 7b888c22a274366c55cc9ab1b8e53d2170f06e8c Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Fri, 15 Jan 2016 08:22:54 +0100 Subject: [PATCH] Zip improvements --- Zip.xcodeproj/project.pbxproj | 4 ++-- Zip/Zip.swift | 18 ++++++++++++++---- Zip/module.modulemap | 5 ----- examples/Sample/Sample/ViewController.swift | 2 ++ 4 files changed, 18 insertions(+), 11 deletions(-) delete mode 100644 Zip/module.modulemap diff --git a/Zip.xcodeproj/project.pbxproj b/Zip.xcodeproj/project.pbxproj index 967d950..fb78bcc 100644 --- a/Zip.xcodeproj/project.pbxproj +++ b/Zip.xcodeproj/project.pbxproj @@ -74,7 +74,6 @@ 3430BB231C484A65001143B5 /* pwd2key.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pwd2key.h; sourceTree = ""; }; 3430BB241C484A65001143B5 /* sha1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha1.c; sourceTree = ""; }; 3430BB251C484A65001143B5 /* sha1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha1.h; sourceTree = ""; }; - 3430BB3E1C484D39001143B5 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = module.modulemap; path = Zip/module.modulemap; sourceTree = SOURCE_ROOT; }; 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 = ""; }; 3430F6241C45C851007473A6 /* ioapi.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ioapi.c; sourceTree = ""; }; @@ -83,6 +82,7 @@ 3430F62A1C45C851007473A6 /* unzip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = unzip.h; sourceTree = ""; }; 3430F62B1C45C851007473A6 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = ""; }; 3430F62C1C45C851007473A6 /* zip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = zip.h; sourceTree = ""; }; + 3433BFCD1C48D3EC006DF000 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; 347E3A741C1DFFB500A11FD3 /* Zip.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Zip.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 347E3A771C1DFFB500A11FD3 /* Zip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Zip.h; sourceTree = ""; }; 347E3A791C1DFFB500A11FD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -146,9 +146,9 @@ 3430BB0F1C484A65001143B5 /* aes */, 3430F6221C45C851007473A6 /* crypt.h */, 3430F6241C45C851007473A6 /* ioapi.c */, + 3433BFCD1C48D3EC006DF000 /* module.modulemap */, 3430F6251C45C851007473A6 /* ioapi.h */, 3430F6291C45C851007473A6 /* unzip.c */, - 3430BB3E1C484D39001143B5 /* module.modulemap */, 3430F62A1C45C851007473A6 /* unzip.h */, 3430F62B1C45C851007473A6 /* zip.c */, 3430F62C1C45C851007473A6 /* zip.h */, diff --git a/Zip/Zip.swift b/Zip/Zip.swift index b45ae5e..9b4aede 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -157,11 +157,13 @@ public class Zip { Quick zip files. - parameter paths: Array of NSURL filepaths. - + - parameter fileName: File name for the resulting zip file. + - throws: rror if zipping fails. */ - public func zipFiles(paths: [NSURL]) throws { - let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + public func zipFiles(paths: [NSURL], fileName: String) throws { + var documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + documentsUrl = documentsUrl.URLByAppendingPathComponent("\(fileName).zip") try self.zipFiles(paths, destination: documentsUrl, password: nil) } @@ -198,7 +200,15 @@ public class Zip { } catch {} let buffer = malloc(chunkSize) - zipOpenNewFileInZip3(zip, fileName!, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password!, 0) + if let password = password, let fileName = fileName { + zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0) + } + else if let fileName = fileName { + zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0) + } + else { + throw ZipError.ZipError + } var len: Int = 0 while (feof(input) == 0) { len = fread(buffer, 1, chunkSize, input) diff --git a/Zip/module.modulemap b/Zip/module.modulemap deleted file mode 100644 index 45a6e58..0000000 --- a/Zip/module.modulemap +++ /dev/null @@ -1,5 +0,0 @@ -module minizip [system][extern_c] { - header "unzip.h" - header "zip.h" - export * -} diff --git a/examples/Sample/Sample/ViewController.swift b/examples/Sample/Sample/ViewController.swift index ae3661a..d22ff4a 100644 --- a/examples/Sample/Sample/ViewController.swift +++ b/examples/Sample/Sample/ViewController.swift @@ -19,6 +19,8 @@ class ViewController: UIViewController { let fileAbsoluteURL = NSURL(string: fileAbsolutePath!)! print(destinationPath) try Zip().unzipFile(fileAbsoluteURL) + try Zip().zipFiles([fileAbsoluteURL], fileName: "zipTest") + } catch { print("oops") -- 2.26.2