From 26f376da28dd08648b3cf84df4f0ddcf20bb2e39 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Sat, 16 Jan 2016 19:24:55 +0100 Subject: [PATCH] Defining quick zip functions --- Zip.xcodeproj/project.pbxproj | 4 + Zip/QuickZip.swift | 82 +++++++++++++++++++++ Zip/Zip.swift | 40 +++------- examples/Sample/Sample/ViewController.swift | 4 +- 4 files changed, 98 insertions(+), 32 deletions(-) create mode 100644 Zip/QuickZip.swift diff --git a/Zip.xcodeproj/project.pbxproj b/Zip.xcodeproj/project.pbxproj index fb78bcc..387784d 100644 --- a/Zip.xcodeproj/project.pbxproj +++ b/Zip.xcodeproj/project.pbxproj @@ -36,6 +36,7 @@ 3430F6341C45C851007473A6 /* unzip.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F62A1C45C851007473A6 /* unzip.h */; }; 3430F6351C45C851007473A6 /* zip.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F62B1C45C851007473A6 /* zip.c */; }; 3430F6361C45C851007473A6 /* zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F62C1C45C851007473A6 /* zip.h */; }; + 3443A3F61C4AB8A3004AD173 /* QuickZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3443A3F51C4AB8A3004AD173 /* QuickZip.swift */; }; 347E3A781C1DFFB500A11FD3 /* Zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 347E3A771C1DFFB500A11FD3 /* Zip.h */; settings = {ATTRIBUTES = (Public, ); }; }; 347E3A7F1C1DFFB500A11FD3 /* Zip.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 347E3A741C1DFFB500A11FD3 /* Zip.framework */; }; 347E3A841C1DFFB500A11FD3 /* ZipTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347E3A831C1DFFB500A11FD3 /* ZipTests.swift */; }; @@ -83,6 +84,7 @@ 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 = ""; }; + 3443A3F51C4AB8A3004AD173 /* QuickZip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuickZip.swift; 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 = ""; }; @@ -182,6 +184,7 @@ 347E3A771C1DFFB500A11FD3 /* Zip.h */, 347E3A791C1DFFB500A11FD3 /* Info.plist */, 347E3AD71C1E04C900A11FD3 /* Zip.swift */, + 3443A3F51C4AB8A3004AD173 /* QuickZip.swift */, ); path = Zip; sourceTree = ""; @@ -330,6 +333,7 @@ 3430BB2F1C484A65001143B5 /* entropy.c in Sources */, 3430F6351C45C851007473A6 /* zip.c in Sources */, 3430BB281C484A65001143B5 /* aescrypt.c in Sources */, + 3443A3F61C4AB8A3004AD173 /* QuickZip.swift in Sources */, 3430BB311C484A65001143B5 /* fileenc.c in Sources */, 3430BB381C484A65001143B5 /* pwd2key.c in Sources */, ); diff --git a/Zip/QuickZip.swift b/Zip/QuickZip.swift new file mode 100644 index 0000000..92bfc94 --- /dev/null +++ b/Zip/QuickZip.swift @@ -0,0 +1,82 @@ +// +// QuickZip.swift +// Zip +// +// Created by Roy Marmelstein on 16/01/2016. +// Copyright © 2016 Roy Marmelstein. All rights reserved. +// + +import Foundation + +extension Zip { + + //MARK: Quick Unzip + + /** + Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name. + + - parameter path: Path of zipped file. NSURL. + + - throws: Error if unzipping fails or if file is not found. Can be printed with a description variable. + + - returns: NSURL of the destination folder. + */ + public func quickUnzipFile(path: NSURL) throws -> NSURL { + return try quickUnzipFile(path, progress: nil) + } + + /** + Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name. + + - parameter path: Path of zipped file. NSURL. + - parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1. + + - throws: Error if unzipping fails or if file is not found. Can be printed with a description variable. + + - returns: NSURL of the destination folder. + */ + public func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL { + guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else { + throw ZipError.UnzipError + } + let directoryName = fileName.stringByReplacingOccurrencesOfString(fileExtension, withString: "") + let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true) + try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress) + return destinationUrl + } + + //MARK: Quick Zip + + /** + Quick zip files. + + - parameter paths: Array of NSURL filepaths. + - parameter fileName: File name for the resulting zip file. + + - throws: rror if zipping fails. + + - returns: NSURL of the destination folder. + */ + public func quickZipFiles(paths: [NSURL], fileName: String) throws -> NSURL { + return try quickZipFiles(paths, fileName: fileName, progress: nil) + } + + /** + Quick zip files. + + - parameter paths: Array of NSURL filepaths. + - parameter fileName: File name for the resulting zip file. + - parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1. + + - throws: rror if zipping fails. + + - returns: NSURL of the destination folder. + */ + public func quickZipFiles(paths: [NSURL], fileName: String, progress: ((progress: Double) -> ())?) throws -> NSURL { + let destinationUrl = documentsUrl.URLByAppendingPathComponent("\(fileName).zip") + try self.zipFiles(paths, destination: destinationUrl, password: nil, progress: progress) + return destinationUrl + } + + +} \ No newline at end of file diff --git a/Zip/Zip.swift b/Zip/Zip.swift index dad7e05..064a05e 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -14,6 +14,7 @@ public enum ZipError: ErrorType { case FileNotFound // File not found case UnzipError // Unzip error case ZipError // Unzip error + case NotAZipFileError // Unzip error /// Description variable public var description: String { @@ -21,6 +22,7 @@ public enum ZipError: ErrorType { case .FileNotFound: return NSLocalizedString("File not found.", comment: "") case .UnzipError: return NSLocalizedString("Failed to unzip zip file.", comment: "") case .ZipError: return NSLocalizedString("Failed to zip file.", comment: "") + case .NotAZipFileError: return NSLocalizedString("The file path does not contain a zip file.", comment: "") } } } @@ -28,6 +30,9 @@ public enum ZipError: ErrorType { public class Zip { + // Documents folder + let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + // MARK: Lifecycle /** @@ -39,20 +44,7 @@ public class Zip { } // MARK: Unzip - - /** - Quick unzip file. Unzips to the app's documents folder. - - - parameter path: Path of zipped file. NSURL. - - - throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable. - */ - public func unzipFile(path: NSURL) throws { - let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - try self.unzipFile(path, destination: documentsUrl, overwrite: true, password: nil, progress:nil) - - } - + /** Unzip file @@ -60,7 +52,8 @@ public class Zip { - parameter destination: Path to unzip to. NSURL. - parameter overwrite: Overwrite bool. - parameter password: Optional password if file is protected. - + - parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1. + - throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable. */ public func unzipFile(path: NSURL, destination: NSURL, overwrite: Bool, password: String?, progress: ((progress: Double) -> ())?) throws { @@ -166,27 +159,14 @@ public class Zip { // MARK: 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], 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, progress: nil) - } - /** Zip files. - parameter paths: Array of NSURL filepaths. - parameter destination: Destination NSURL, should lead to a .zip filepath. - parameter password: Password string. Optional. - + - parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1. + - throws: Error if zipping fails. */ public func zipFiles(paths: [NSURL], destination: NSURL, password: String?, progress: ((progress: Double) -> ())?) throws { diff --git a/examples/Sample/Sample/ViewController.swift b/examples/Sample/Sample/ViewController.swift index d22ff4a..41436ac 100644 --- a/examples/Sample/Sample/ViewController.swift +++ b/examples/Sample/Sample/ViewController.swift @@ -18,8 +18,8 @@ class ViewController: UIViewController { let fileAbsolutePath = NSBundle.mainBundle().pathForResource("master", ofType: "zip") let fileAbsoluteURL = NSURL(string: fileAbsolutePath!)! print(destinationPath) - try Zip().unzipFile(fileAbsoluteURL) - try Zip().zipFiles([fileAbsoluteURL], fileName: "zipTest") + try Zip().quickUnzipFile(fileAbsoluteURL) + try Zip().quickZipFiles([fileAbsoluteURL], fileName: "zipTest") } catch { -- 2.26.2