Commit a81c88ac authored by Roy Marmelstein's avatar Roy Marmelstein

Make all zip functions class functions for nicer syntax

parent a9d9a2bb
......@@ -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)
......
......@@ -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 {
......
......@@ -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()
......
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