From c8d9230bb9faf5a378681e538487c742eeef698a Mon Sep 17 00:00:00 2001 From: Pavel Hlavnicka Date: Mon, 8 Aug 2016 16:25:43 +0200 Subject: [PATCH] fixes for XC8 beta 4 --- .../xcshareddata/Zip.xcscmblueprint | 2 +- Zip/QuickZip.swift | 13 +-- Zip/Zip.swift | 24 +++--- Zip/ZipUtilities.swift | 26 +++--- ZipTests/ZipTests.swift | 82 +++++++++---------- 5 files changed, 70 insertions(+), 77 deletions(-) diff --git a/Zip.xcodeproj/project.xcworkspace/xcshareddata/Zip.xcscmblueprint b/Zip.xcodeproj/project.xcworkspace/xcshareddata/Zip.xcscmblueprint index 0b4f060..0542877 100644 --- a/Zip.xcodeproj/project.xcworkspace/xcshareddata/Zip.xcscmblueprint +++ b/Zip.xcodeproj/project.xcworkspace/xcshareddata/Zip.xcscmblueprint @@ -10,7 +10,7 @@ "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "F3707899-72AE-49DA-9BDD-5CB0B64CF03A", "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { "8DA5B175D3FDB92A3B3CCBD4109A734F1316A3DD" : "Zip\/Zip\/minizip\/", - "3DD768C8AB2D6A2647C9EF99992D3CC5820E77C4" : "Zip\/" + "3DD768C8AB2D6A2647C9EF99992D3CC5820E77C4" : "Zip-swift3\/" }, "DVTSourceControlWorkspaceBlueprintNameKey" : "Zip", "DVTSourceControlWorkspaceBlueprintVersion" : 204, diff --git a/Zip/QuickZip.swift b/Zip/QuickZip.swift index 6e4ad0f..02ba68c 100644 --- a/Zip/QuickZip.swift +++ b/Zip/QuickZip.swift @@ -39,13 +39,14 @@ extension Zip { */ public class func quickUnzipFile(_ path: URL, progress: ((progress: Double) -> ())?) throws -> URL { let fileManager = FileManager.default - guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else { - throw ZipError.unzipFail - } + + let fileExtension = path.pathExtension + let fileName = path.lastPathComponent + let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") - let documentsUrl = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as URL + let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL do { - let destinationUrl = try documentsUrl.appendingPathComponent(directoryName, isDirectory: true) + let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress) return destinationUrl }catch{ @@ -86,7 +87,7 @@ extension Zip { */ public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((progress: Double) -> ())?) throws -> URL { let fileManager = FileManager.default - let documentsUrl = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as URL + let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL let destinationUrl = try! documentsUrl.appendingPathComponent("\(fileName).zip") try self.zipFiles(paths, zipFilePath: destinationUrl, password: nil, progress: progress) return destinationUrl diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 7f69389..0097da9 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -10,7 +10,7 @@ import Foundation import minizip /// Zip error type -public enum ZipError: ErrorProtocol { +public enum ZipError: Error { /// File not found case fileNotFound /// Unzip fail @@ -68,9 +68,8 @@ public class Zip { let fileManager = FileManager.default // Check whether a zip file exists at path. - guard let path = zipFilePath.path, destination.path != nil else { - throw ZipError.fileNotFound - } + let path = zipFilePath.path + if fileManager.fileExists(atPath: path) == false || fileExtensionIsInvalid(zipFilePath.pathExtension) { throw ZipError.fileNotFound } @@ -121,7 +120,8 @@ public class Zip { } currentPosition += Double(fileInfo.compressed_size) let fileNameSize = Int(fileInfo.size_filename) + 1 - let fileName = UnsafeMutablePointer(allocatingCapacity: fileNameSize) + //let fileName = UnsafeMutablePointer(allocatingCapacity: fileNameSize) + let fileName = UnsafeMutablePointer.allocate(capacity: fileNameSize) unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) fileName[Int(fileInfo.size_filename)] = 0 @@ -140,9 +140,9 @@ public class Zip { if pathString.rangeOfCharacter(from: CharacterSet(charactersIn: "/\\")) != nil { pathString = pathString.replacingOccurrences(of: "\\", with: "/") } - guard let fullPath = try! destination.appendingPathComponent(pathString).path else { - throw ZipError.unzipFail - } + + let fullPath = destination.appendingPathComponent(pathString).path + let creationDate = Date() let directoryAttributes = [FileAttributeKey.creationDate.rawValue : creationDate, FileAttributeKey.modificationDate.rawValue : creationDate] @@ -215,9 +215,7 @@ public class Zip { let fileManager = FileManager.default // Check whether a zip file exists at path. - guard let destinationPath = zipFilePath.path else { - throw ZipError.fileNotFound - } + let destinationPath = zipFilePath.path // Process zip paths let processedPaths = ZipUtilities().processZipPaths(paths) @@ -252,7 +250,7 @@ public class Zip { let filePath = path.filePath() var isDirectory: ObjCBool = false fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) - if !isDirectory { + if !isDirectory.boolValue { let input = fopen(filePath, "r") if input == nil { throw ZipError.zipFail @@ -262,7 +260,7 @@ public class Zip { do { let fileAttributes = try fileManager.attributesOfItem(atPath: filePath) if let fileDate = fileAttributes[FileAttributeKey.modificationDate] as? Date { - let components = Calendar.current.components([.year, .month, .day, .hour, .minute, .second], from: fileDate) + let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fileDate) zipInfo.tmz_date.tm_sec = UInt32(components.second!) zipInfo.tmz_date.tm_min = UInt32(components.minute!) zipInfo.tmz_date.tm_hour = UInt32(components.hour!) diff --git a/Zip/ZipUtilities.swift b/Zip/ZipUtilities.swift index bed1574..921b0c5 100644 --- a/Zip/ZipUtilities.swift +++ b/Zip/ZipUtilities.swift @@ -21,12 +21,7 @@ internal class ZipUtilities { let fileName: String? func filePath() -> String { - if let filePath = filePathURL.path { - return filePath - } - else { - return String() - } + return filePathURL.path } } @@ -42,12 +37,10 @@ internal class ZipUtilities { internal func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath]{ var processedFilePaths = [ProcessedFilePath]() for path in paths { - guard let filePath = path.path else { - continue - } + let filePath = path.path var isDirectory: ObjCBool = false fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) - if !isDirectory { + if !isDirectory.boolValue { let processedPath = ProcessedFilePath(filePathURL: path, fileName: path.lastPathComponent) processedFilePaths.append(processedPath) } @@ -69,15 +62,16 @@ internal class ZipUtilities { */ internal func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] { var processedFilePaths = [ProcessedFilePath]() - if let directoryPath = directory.path, let enumerator = fileManager.enumerator(atPath: directoryPath) { + let directoryPath = directory.path + if let enumerator = fileManager.enumerator(atPath: directoryPath) { while let filePathComponent = enumerator.nextObject() as? String { - let path = try! directory.appendingPathComponent(filePathComponent) - guard let filePath = path.path, let directoryName = directory.lastPathComponent else { - continue - } + let path = directory.appendingPathComponent(filePathComponent) + let filePath = path.path + let directoryName = directory.lastPathComponent + var isDirectory: ObjCBool = false fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) - if !isDirectory { + if !isDirectory.boolValue { let fileName = (directoryName as NSString).appendingPathComponent(filePathComponent) let processedPath = ProcessedFilePath(filePathURL: path, fileName: fileName) processedFilePaths.append(processedPath) diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index bad3179..ed4334d 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -21,10 +21,10 @@ class ZipTests: XCTestCase { func testQuickUnzip() { do { - let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! + let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")! let destinationURL = try Zip.quickUnzipFile(filePath) let fileManager = FileManager.default - XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path)) } catch { XCTFail() @@ -37,7 +37,7 @@ class ZipTests: XCTestCase { let filePath = NSURL(string:"\(filePathURL!)/bb9.zip") let destinationURL = try Zip.quickUnzipFile(filePath! as URL) let fileManager = FileManager.default - XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path)) } catch { XCTAssert(true) @@ -46,10 +46,10 @@ class ZipTests: XCTestCase { func testQuickUnzipNonZipPath() { do { - let filePath = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let filePath = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! let destinationURL = try Zip.quickUnzipFile(filePath) let fileManager = FileManager.default - XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path)) } catch { XCTAssert(true) @@ -58,8 +58,8 @@ class ZipTests: XCTestCase { func testQuickUnzipProgress() { do { - let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! - try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in + let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")! + _ = try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in XCTAssert(true) }) } @@ -73,7 +73,7 @@ class ZipTests: XCTestCase { let filePath = NSURL(string: "http://www.google.com/google.zip")! let destinationURL = try Zip.quickUnzipFile(filePath as URL) let fileManager = FileManager.default - XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path)) } catch { XCTAssert(true) @@ -82,8 +82,8 @@ class ZipTests: XCTestCase { func testUnzip() { do { - let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! - let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")! + let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: { (progress) -> () in print(progress) @@ -102,8 +102,8 @@ class ZipTests: XCTestCase { let progress = Progress() progress.totalUnitCount = 1 - let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! - let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")! + let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL progress.becomeCurrent(withPendingUnitCount: 1) try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: nil) @@ -122,9 +122,9 @@ class ZipTests: XCTestCase { let progress = Progress() progress.totalUnitCount = 1 - let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") progress.becomeCurrent(withPendingUnitCount: 1) @@ -141,11 +141,11 @@ class ZipTests: XCTestCase { func testQuickZip() { do { - let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! + let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")! let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive") let fileManager = FileManager.default - XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path)) } catch { XCTFail() @@ -155,19 +155,19 @@ class ZipTests: XCTestCase { func testQuickZipFolder() { do { let fileManager = FileManager.default - let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! - let folderURL = try Bundle(for: ZipTests.self).bundleURL.appendingPathComponent("Directory") - let targetImageURL1 = try folderURL.appendingPathComponent("3crBXeO.gif") - let targetImageURL2 = try folderURL.appendingPathComponent("kYkLkPf.gif") - if fileManager.fileExists(atPath:folderURL.path!) { + let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")! + let folderURL = Bundle(for: ZipTests.self).bundleURL.appendingPathComponent("Directory") + let targetImageURL1 = folderURL.appendingPathComponent("3crBXeO.gif") + let targetImageURL2 = folderURL.appendingPathComponent("kYkLkPf.gif") + if fileManager.fileExists(atPath:folderURL.path) { try fileManager.removeItem(at: folderURL) } try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false, attributes: nil) try fileManager.copyItem(at: imageURL1, to: targetImageURL1) try fileManager.copyItem(at: imageURL2, to: targetImageURL2) let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory") - XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path)) } catch { XCTFail() @@ -177,15 +177,15 @@ class ZipTests: XCTestCase { func testZip() { do { - let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: nil, progress: { (progress) -> () in print(progress) }) let fileManager = FileManager.default - XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!)) + XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path)!)) } catch { XCTFail() @@ -194,23 +194,23 @@ class ZipTests: XCTestCase { func testZipUnzipPassword() { do { - let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: "password", progress: { (progress) -> () in print(progress) }) let fileManager = FileManager.default - XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!)) + XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path)!)) guard let fileExtension = zipFilePath?.pathExtension, let fileName = zipFilePath?.lastPathComponent else { throw ZipError.unzipFail } let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") - let documentsUrl = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) try Zip.unzipFile(zipFilePath!, destination: destinationUrl!, overwrite: true, password: "password", progress: nil) - XCTAssertTrue(fileManager.fileExists(atPath:(destinationUrl?.path!)!)) + XCTAssertTrue(fileManager.fileExists(atPath:(destinationUrl?.path)!)) } catch { XCTFail() @@ -220,15 +220,15 @@ class ZipTests: XCTestCase { func testQuickUnzipSubDir() { do { - let bookURL = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! + let bookURL = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")! let unzipDestination = try Zip.quickUnzipFile(bookURL) let fileManager = FileManager.default - let subDir = try unzipDestination.appendingPathComponent("subDir") - let imageURL = try subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif") + let subDir = unzipDestination.appendingPathComponent("subDir") + let imageURL = subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif") - XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path!)) - XCTAssertTrue(fileManager.fileExists(atPath:subDir.path!)) - XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path)) + XCTAssertTrue(fileManager.fileExists(atPath:subDir.path)) + XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path)) } catch { XCTFail() } -- 2.26.2