diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 43d804a2aa7fba35f58d74466fa8c873b993c47e..e062b14267567dd51c8f8d1e08c1ca3ac4473627 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -66,7 +66,7 @@ public class Zip { guard let path = zipFilePath.path where destination.path != nil else { throw ZipError.FileNotFound } - if fileManager.fileExistsAtPath(path) == false || zipFilePath.pathExtension != "zip" { + if fileManager.fileExistsAtPath(path) == false || fileExtensionIsInvalid(zipFilePath.pathExtension) { throw ZipError.FileNotFound } @@ -303,6 +303,18 @@ public class Zip { progressTracker.completedUnitCount = Int64(totalSize) } - + /** + Check if file extension is invalid. + + - parameter fileExtension: A file extension. + + - returns: false if the extension is "zip" or "cbz", otherwise true. + */ + internal class func fileExtensionIsInvalid(fileExtension: String?) -> Bool { + + guard let fileExtension = fileExtension else { return true } + + return !["zip", "cbz"].contains(fileExtension) + } } \ No newline at end of file diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index f80b6e4d47577d7546a43d6523930524a0b9ab85..2b59732e041af05d219f3cdb439b93ba2bdb0cc6 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -208,6 +208,16 @@ class ZipTests: XCTestCase { XCTFail() } } + + func testFileExtensionIsInvalidForValidUrl() { + let fileUrl = NSURL(string: "file.cbz") + let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension) + XCTAssertFalse(result) + } - + func testFileExtensionIsInvalidForInvalidUrl() { + let fileUrl = NSURL(string: "file.xyz") + let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension) + XCTAssertTrue(result) + } }