Commit c8d9230b authored by Pavel Hlavnicka's avatar Pavel Hlavnicka

fixes for XC8 beta 4

parent 9d44d606
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "F3707899-72AE-49DA-9BDD-5CB0B64CF03A", "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "F3707899-72AE-49DA-9BDD-5CB0B64CF03A",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"8DA5B175D3FDB92A3B3CCBD4109A734F1316A3DD" : "Zip\/Zip\/minizip\/", "8DA5B175D3FDB92A3B3CCBD4109A734F1316A3DD" : "Zip\/Zip\/minizip\/",
"3DD768C8AB2D6A2647C9EF99992D3CC5820E77C4" : "Zip\/" "3DD768C8AB2D6A2647C9EF99992D3CC5820E77C4" : "Zip-swift3\/"
}, },
"DVTSourceControlWorkspaceBlueprintNameKey" : "Zip", "DVTSourceControlWorkspaceBlueprintNameKey" : "Zip",
"DVTSourceControlWorkspaceBlueprintVersion" : 204, "DVTSourceControlWorkspaceBlueprintVersion" : 204,
......
...@@ -39,13 +39,14 @@ extension Zip { ...@@ -39,13 +39,14 @@ extension Zip {
*/ */
public class func quickUnzipFile(_ path: URL, progress: ((progress: Double) -> ())?) throws -> URL { public class func quickUnzipFile(_ path: URL, progress: ((progress: Double) -> ())?) throws -> URL {
let fileManager = FileManager.default 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 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 { 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) try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress)
return destinationUrl return destinationUrl
}catch{ }catch{
...@@ -86,7 +87,7 @@ extension Zip { ...@@ -86,7 +87,7 @@ extension Zip {
*/ */
public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((progress: Double) -> ())?) throws -> URL { public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((progress: Double) -> ())?) throws -> URL {
let fileManager = FileManager.default 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") let destinationUrl = try! documentsUrl.appendingPathComponent("\(fileName).zip")
try self.zipFiles(paths, zipFilePath: destinationUrl, password: nil, progress: progress) try self.zipFiles(paths, zipFilePath: destinationUrl, password: nil, progress: progress)
return destinationUrl return destinationUrl
......
...@@ -10,7 +10,7 @@ import Foundation ...@@ -10,7 +10,7 @@ import Foundation
import minizip import minizip
/// Zip error type /// Zip error type
public enum ZipError: ErrorProtocol { public enum ZipError: Error {
/// File not found /// File not found
case fileNotFound case fileNotFound
/// Unzip fail /// Unzip fail
...@@ -68,9 +68,8 @@ public class Zip { ...@@ -68,9 +68,8 @@ public class Zip {
let fileManager = FileManager.default let fileManager = FileManager.default
// Check whether a zip file exists at path. // Check whether a zip file exists at path.
guard let path = zipFilePath.path, destination.path != nil else { let path = zipFilePath.path
throw ZipError.fileNotFound
}
if fileManager.fileExists(atPath: path) == false || fileExtensionIsInvalid(zipFilePath.pathExtension) { if fileManager.fileExists(atPath: path) == false || fileExtensionIsInvalid(zipFilePath.pathExtension) {
throw ZipError.fileNotFound throw ZipError.fileNotFound
} }
...@@ -121,7 +120,8 @@ public class Zip { ...@@ -121,7 +120,8 @@ public class Zip {
} }
currentPosition += Double(fileInfo.compressed_size) currentPosition += Double(fileInfo.compressed_size)
let fileNameSize = Int(fileInfo.size_filename) + 1 let fileNameSize = Int(fileInfo.size_filename) + 1
let fileName = UnsafeMutablePointer<CChar>(allocatingCapacity: fileNameSize) //let fileName = UnsafeMutablePointer<CChar>(allocatingCapacity: fileNameSize)
let fileName = UnsafeMutablePointer<CChar>.allocate(capacity: fileNameSize)
unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0) unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0)
fileName[Int(fileInfo.size_filename)] = 0 fileName[Int(fileInfo.size_filename)] = 0
...@@ -140,9 +140,9 @@ public class Zip { ...@@ -140,9 +140,9 @@ public class Zip {
if pathString.rangeOfCharacter(from: CharacterSet(charactersIn: "/\\")) != nil { if pathString.rangeOfCharacter(from: CharacterSet(charactersIn: "/\\")) != nil {
pathString = pathString.replacingOccurrences(of: "\\", with: "/") 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 creationDate = Date()
let directoryAttributes = [FileAttributeKey.creationDate.rawValue : creationDate, let directoryAttributes = [FileAttributeKey.creationDate.rawValue : creationDate,
FileAttributeKey.modificationDate.rawValue : creationDate] FileAttributeKey.modificationDate.rawValue : creationDate]
...@@ -215,9 +215,7 @@ public class Zip { ...@@ -215,9 +215,7 @@ public class Zip {
let fileManager = FileManager.default let fileManager = FileManager.default
// Check whether a zip file exists at path. // Check whether a zip file exists at path.
guard let destinationPath = zipFilePath.path else { let destinationPath = zipFilePath.path
throw ZipError.fileNotFound
}
// Process zip paths // Process zip paths
let processedPaths = ZipUtilities().processZipPaths(paths) let processedPaths = ZipUtilities().processZipPaths(paths)
...@@ -252,7 +250,7 @@ public class Zip { ...@@ -252,7 +250,7 @@ public class Zip {
let filePath = path.filePath() let filePath = path.filePath()
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
if !isDirectory { if !isDirectory.boolValue {
let input = fopen(filePath, "r") let input = fopen(filePath, "r")
if input == nil { if input == nil {
throw ZipError.zipFail throw ZipError.zipFail
...@@ -262,7 +260,7 @@ public class Zip { ...@@ -262,7 +260,7 @@ public class Zip {
do { do {
let fileAttributes = try fileManager.attributesOfItem(atPath: filePath) let fileAttributes = try fileManager.attributesOfItem(atPath: filePath)
if let fileDate = fileAttributes[FileAttributeKey.modificationDate] as? Date { 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_sec = UInt32(components.second!)
zipInfo.tmz_date.tm_min = UInt32(components.minute!) zipInfo.tmz_date.tm_min = UInt32(components.minute!)
zipInfo.tmz_date.tm_hour = UInt32(components.hour!) zipInfo.tmz_date.tm_hour = UInt32(components.hour!)
......
...@@ -21,12 +21,7 @@ internal class ZipUtilities { ...@@ -21,12 +21,7 @@ internal class ZipUtilities {
let fileName: String? let fileName: String?
func filePath() -> String { func filePath() -> String {
if let filePath = filePathURL.path { return filePathURL.path
return filePath
}
else {
return String()
}
} }
} }
...@@ -42,12 +37,10 @@ internal class ZipUtilities { ...@@ -42,12 +37,10 @@ internal class ZipUtilities {
internal func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath]{ internal func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath]{
var processedFilePaths = [ProcessedFilePath]() var processedFilePaths = [ProcessedFilePath]()
for path in paths { for path in paths {
guard let filePath = path.path else { let filePath = path.path
continue
}
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
if !isDirectory { if !isDirectory.boolValue {
let processedPath = ProcessedFilePath(filePathURL: path, fileName: path.lastPathComponent) let processedPath = ProcessedFilePath(filePathURL: path, fileName: path.lastPathComponent)
processedFilePaths.append(processedPath) processedFilePaths.append(processedPath)
} }
...@@ -69,15 +62,16 @@ internal class ZipUtilities { ...@@ -69,15 +62,16 @@ internal class ZipUtilities {
*/ */
internal func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] { internal func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] {
var processedFilePaths = [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 { while let filePathComponent = enumerator.nextObject() as? String {
let path = try! directory.appendingPathComponent(filePathComponent) let path = directory.appendingPathComponent(filePathComponent)
guard let filePath = path.path, let directoryName = directory.lastPathComponent else { let filePath = path.path
continue let directoryName = directory.lastPathComponent
}
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
if !isDirectory { if !isDirectory.boolValue {
let fileName = (directoryName as NSString).appendingPathComponent(filePathComponent) let fileName = (directoryName as NSString).appendingPathComponent(filePathComponent)
let processedPath = ProcessedFilePath(filePathURL: path, fileName: fileName) let processedPath = ProcessedFilePath(filePathURL: path, fileName: fileName)
processedFilePaths.append(processedPath) processedFilePaths.append(processedPath)
......
...@@ -21,10 +21,10 @@ class ZipTests: XCTestCase { ...@@ -21,10 +21,10 @@ class ZipTests: XCTestCase {
func testQuickUnzip() { func testQuickUnzip() {
do { 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 destinationURL = try Zip.quickUnzipFile(filePath)
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path!)) XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path))
} }
catch { catch {
XCTFail() XCTFail()
...@@ -37,7 +37,7 @@ class ZipTests: XCTestCase { ...@@ -37,7 +37,7 @@ class ZipTests: XCTestCase {
let filePath = NSURL(string:"\(filePathURL!)/bb9.zip") let filePath = NSURL(string:"\(filePathURL!)/bb9.zip")
let destinationURL = try Zip.quickUnzipFile(filePath! as URL) let destinationURL = try Zip.quickUnzipFile(filePath! as URL)
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path))
} }
catch { catch {
XCTAssert(true) XCTAssert(true)
...@@ -46,10 +46,10 @@ class ZipTests: XCTestCase { ...@@ -46,10 +46,10 @@ class ZipTests: XCTestCase {
func testQuickUnzipNonZipPath() { func testQuickUnzipNonZipPath() {
do { 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 destinationURL = try Zip.quickUnzipFile(filePath)
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path))
} }
catch { catch {
XCTAssert(true) XCTAssert(true)
...@@ -58,8 +58,8 @@ class ZipTests: XCTestCase { ...@@ -58,8 +58,8 @@ class ZipTests: XCTestCase {
func testQuickUnzipProgress() { func testQuickUnzipProgress() {
do { do {
let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")!
try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in _ = try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in
XCTAssert(true) XCTAssert(true)
}) })
} }
...@@ -73,7 +73,7 @@ class ZipTests: XCTestCase { ...@@ -73,7 +73,7 @@ class ZipTests: XCTestCase {
let filePath = NSURL(string: "http://www.google.com/google.zip")! let filePath = NSURL(string: "http://www.google.com/google.zip")!
let destinationURL = try Zip.quickUnzipFile(filePath as URL) let destinationURL = try Zip.quickUnzipFile(filePath as URL)
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path))
} }
catch { catch {
XCTAssert(true) XCTAssert(true)
...@@ -82,8 +82,8 @@ class ZipTests: XCTestCase { ...@@ -82,8 +82,8 @@ class ZipTests: XCTestCase {
func testUnzip() { func testUnzip() {
do { do {
let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")!
let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL 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 try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: { (progress) -> () in
print(progress) print(progress)
...@@ -102,8 +102,8 @@ class ZipTests: XCTestCase { ...@@ -102,8 +102,8 @@ class ZipTests: XCTestCase {
let progress = Progress() let progress = Progress()
progress.totalUnitCount = 1 progress.totalUnitCount = 1
let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! let filePath = Bundle(for: ZipTests.self).url(forResource: "bb8", withExtension: "zip")!
let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
progress.becomeCurrent(withPendingUnitCount: 1) progress.becomeCurrent(withPendingUnitCount: 1)
try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: nil) try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: nil)
...@@ -122,9 +122,9 @@ class ZipTests: XCTestCase { ...@@ -122,9 +122,9 @@ class ZipTests: XCTestCase {
let progress = Progress() let progress = Progress()
progress.totalUnitCount = 1 progress.totalUnitCount = 1
let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")!
let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")!
let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") let zipFilePath = documentsFolder.appendingPathComponent("archive.zip")
progress.becomeCurrent(withPendingUnitCount: 1) progress.becomeCurrent(withPendingUnitCount: 1)
...@@ -141,11 +141,11 @@ class ZipTests: XCTestCase { ...@@ -141,11 +141,11 @@ class ZipTests: XCTestCase {
func testQuickZip() { func testQuickZip() {
do { do {
let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")!
let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")!
let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive") let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive")
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path))
} }
catch { catch {
XCTFail() XCTFail()
...@@ -155,19 +155,19 @@ class ZipTests: XCTestCase { ...@@ -155,19 +155,19 @@ class ZipTests: XCTestCase {
func testQuickZipFolder() { func testQuickZipFolder() {
do { do {
let fileManager = FileManager.default let fileManager = FileManager.default
let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")!
let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")!
let folderURL = try Bundle(for: ZipTests.self).bundleURL.appendingPathComponent("Directory") let folderURL = Bundle(for: ZipTests.self).bundleURL.appendingPathComponent("Directory")
let targetImageURL1 = try folderURL.appendingPathComponent("3crBXeO.gif") let targetImageURL1 = folderURL.appendingPathComponent("3crBXeO.gif")
let targetImageURL2 = try folderURL.appendingPathComponent("kYkLkPf.gif") let targetImageURL2 = folderURL.appendingPathComponent("kYkLkPf.gif")
if fileManager.fileExists(atPath:folderURL.path!) { if fileManager.fileExists(atPath:folderURL.path) {
try fileManager.removeItem(at: folderURL) try fileManager.removeItem(at: folderURL)
} }
try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false, attributes: nil) try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false, attributes: nil)
try fileManager.copyItem(at: imageURL1, to: targetImageURL1) try fileManager.copyItem(at: imageURL1, to: targetImageURL1)
try fileManager.copyItem(at: imageURL2, to: targetImageURL2) try fileManager.copyItem(at: imageURL2, to: targetImageURL2)
let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory") let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory")
XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path))
} }
catch { catch {
XCTFail() XCTFail()
...@@ -177,15 +177,15 @@ class ZipTests: XCTestCase { ...@@ -177,15 +177,15 @@ class ZipTests: XCTestCase {
func testZip() { func testZip() {
do { do {
let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")!
let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")!
let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") let zipFilePath = documentsFolder.appendingPathComponent("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) print(progress)
}) })
let fileManager = FileManager.default let fileManager = FileManager.default
XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!)) XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path)!))
} }
catch { catch {
XCTFail() XCTFail()
...@@ -194,23 +194,23 @@ class ZipTests: XCTestCase { ...@@ -194,23 +194,23 @@ class ZipTests: XCTestCase {
func testZipUnzipPassword() { func testZipUnzipPassword() {
do { do {
let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let imageURL1 = Bundle(for: ZipTests.self).url(forResource: "3crBXeO", withExtension: "gif")!
let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let imageURL2 = Bundle(for: ZipTests.self).url(forResource: "kYkLkPf", withExtension: "gif")!
let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") let zipFilePath = documentsFolder.appendingPathComponent("archive.zip")
try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: "password", progress: { (progress) -> () in try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: "password", progress: { (progress) -> () in
print(progress) print(progress)
}) })
let fileManager = FileManager.default 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 { guard let fileExtension = zipFilePath?.pathExtension, let fileName = zipFilePath?.lastPathComponent else {
throw ZipError.unzipFail throw ZipError.unzipFail
} }
let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") 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) let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true)
try Zip.unzipFile(zipFilePath!, destination: destinationUrl!, overwrite: true, password: "password", progress: nil) 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 { catch {
XCTFail() XCTFail()
...@@ -220,15 +220,15 @@ class ZipTests: XCTestCase { ...@@ -220,15 +220,15 @@ class ZipTests: XCTestCase {
func testQuickUnzipSubDir() { func testQuickUnzipSubDir() {
do { 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 unzipDestination = try Zip.quickUnzipFile(bookURL)
let fileManager = FileManager.default let fileManager = FileManager.default
let subDir = try unzipDestination.appendingPathComponent("subDir") let subDir = unzipDestination.appendingPathComponent("subDir")
let imageURL = try subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif") let imageURL = subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif")
XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path!)) XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path))
XCTAssertTrue(fileManager.fileExists(atPath:subDir.path!)) XCTAssertTrue(fileManager.fileExists(atPath:subDir.path))
XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path!)) XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path))
} catch { } catch {
XCTFail() XCTFail()
} }
......
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