From a81c88acd4ba0e40300828d0f43b65742394f38d Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Tue, 19 Jan 2016 20:56:26 +0100 Subject: [PATCH] Make all zip functions class functions for nicer syntax --- Zip/QuickZip.swift | 10 ++++++---- Zip/Zip.swift | 13 ++++++++----- ZipTests/ZipTests.swift | 16 ++++++++-------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Zip/QuickZip.swift b/Zip/QuickZip.swift index 4eb226e..ebb2274 100644 --- a/Zip/QuickZip.swift +++ b/Zip/QuickZip.swift @@ -21,7 +21,7 @@ extension Zip { - returns: NSURL of the destination folder. */ - public func quickUnzipFile(path: NSURL) throws -> NSURL { + public class func quickUnzipFile(path: NSURL) throws -> NSURL { return try quickUnzipFile(path, progress: nil) } @@ -35,7 +35,8 @@ extension Zip { - returns: NSURL of the destination folder. */ - public func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL { + public class func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL { + let fileManager = NSFileManager.defaultManager() guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else { throw ZipError.UnzipFail } @@ -58,7 +59,7 @@ extension Zip { - returns: NSURL of the destination folder. */ - public func quickZipFiles(paths: [NSURL], fileName: String) throws -> NSURL { + public class func quickZipFiles(paths: [NSURL], fileName: String) throws -> NSURL { return try quickZipFiles(paths, fileName: fileName, progress: nil) } @@ -73,7 +74,8 @@ extension Zip { - returns: NSURL of the destination folder. */ - public func quickZipFiles(paths: [NSURL], fileName: String, progress: ((progress: Double) -> ())?) throws -> NSURL { + public class func quickZipFiles(paths: [NSURL], fileName: String, progress: ((progress: Double) -> ())?) throws -> NSURL { + let fileManager = NSFileManager.defaultManager() let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL let destinationUrl = documentsUrl.URLByAppendingPathComponent("\(fileName).zip") try self.zipFiles(paths, zipFilePath: destinationUrl, password: nil, progress: progress) diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 3065cc4..0cedac4 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -31,9 +31,6 @@ public enum ZipError: ErrorType { /// Zip class public class Zip { - // File manager - let fileManager = NSFileManager.defaultManager() - // MARK: Lifecycle /** @@ -58,8 +55,11 @@ public class Zip { - throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable. */ - public func unzipFile(zipFilePath: NSURL, destination: NSURL, overwrite: Bool, password: String?, progress: ((progress: Double) -> ())?) throws { + public class func unzipFile(zipFilePath: NSURL, destination: NSURL, overwrite: Bool, password: String?, progress: ((progress: Double) -> ())?) throws { + // File manager + let fileManager = NSFileManager.defaultManager() + // Check whether a zip file exists at path. guard let path = zipFilePath.path, let destinationPath = destination.path else { throw ZipError.FileNotFound @@ -185,7 +185,10 @@ public class Zip { - throws: Error if zipping fails. */ - public func zipFiles(paths: [NSURL], zipFilePath: NSURL, password: String?, progress: ((progress: Double) -> ())?) throws { + public class func zipFiles(paths: [NSURL], zipFilePath: NSURL, password: String?, progress: ((progress: Double) -> ())?) throws { + + // File manager + let fileManager = NSFileManager.defaultManager() // Check whether a zip file exists at path. guard let destinationPath = zipFilePath.path else { diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index ee9f16a..e19355a 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -22,7 +22,7 @@ class ZipTests: XCTestCase { func testQuickUnzip() { do { let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - let destinationURL = try Zip().quickUnzipFile(filePath) + let destinationURL = try Zip.quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -35,7 +35,7 @@ class ZipTests: XCTestCase { do { let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath let filePath = NSURL(string:"\(filePathURL!)/bb9.zip") - let destinationURL = try Zip().quickUnzipFile(filePath!) + let destinationURL = try Zip.quickUnzipFile(filePath!) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -47,7 +47,7 @@ class ZipTests: XCTestCase { func testQuickUnzipNonZipPath() { do { let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let destinationURL = try Zip().quickUnzipFile(filePath) + let destinationURL = try Zip.quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -59,7 +59,7 @@ class ZipTests: XCTestCase { func testQuickUnzipProgress() { do { let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - try Zip().quickUnzipFile(filePath, progress: { (progress) -> () in + try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in XCTAssert(true) }) } @@ -71,7 +71,7 @@ class ZipTests: XCTestCase { func testQuickUnzipOnlineURL() { do { let filePath = NSURL(string: "http://www.google.com/google.zip")! - let destinationURL = try Zip().quickUnzipFile(filePath) + let destinationURL = try Zip.quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -84,7 +84,7 @@ class ZipTests: XCTestCase { do { let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - try Zip().unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: { (progress) -> () in + try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: { (progress) -> () in print(progress) }) let fileManager = NSFileManager.defaultManager() @@ -99,7 +99,7 @@ class ZipTests: XCTestCase { do { let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! - let destinationURL = try Zip().quickZipFiles([imageURL1, imageURL2], fileName: "archive") + let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive") let fileManager = NSFileManager.defaultManager() XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -114,7 +114,7 @@ class ZipTests: XCTestCase { let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip") - try Zip().zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: { (progress) -> () in + try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: { (progress) -> () in print(progress) }) let fileManager = NSFileManager.defaultManager() -- 2.26.2