Repository navigation
Expand file tree
/
Copy pathZipTests.swift
More file actions
249 lines (216 loc) · 10.2 KB
/
Copy pathZipTests.swift
File metadata and controls
249 lines (216 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// ZipTests.swift
// ZipTests
//
// Created by Roy Marmelstein on 13/12/2015.
// Copyright © 2015 Roy Marmelstein. All rights reserved.
//
import XCTest
@testable import Zip
class ZipTests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testQuickUnzip() {
do {
let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
let destinationURL = try Zip.quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTFail()
}
}
func testQuickUnzipNonExistingPath() {
do {
let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath
let filePath = NSURL(string:"\(filePathURL!)/bb9.zip")
let destinationURL = try Zip.quickUnzipFile(filePath!)
let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTAssert(true)
}
}
func testQuickUnzipNonZipPath() {
do {
let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
let destinationURL = try Zip.quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTAssert(true)
}
}
func testQuickUnzipProgress() {
do {
let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in
XCTAssert(true)
})
}
catch {
XCTFail()
}
}
func testQuickUnzipOnlineURL() {
do {
let filePath = NSURL(string: "http://www.google.com/google.zip")!
let destinationURL = try Zip.quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTAssert(true)
}
}
func testUnzip() {
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
print(progress)
})
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(documentsFolder.path!))
}
catch {
XCTFail()
}
}
func testImplicitProgressUnzip() {
do {
let progress = NSProgress()
progress.totalUnitCount = 1
let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
progress.becomeCurrentWithPendingUnitCount(1)
try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: nil)
progress.resignCurrent()
XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
}
catch {
XCTFail()
}
}
func testImplicitProgressZip() {
do {
let progress = NSProgress()
progress.totalUnitCount = 1
let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
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")
progress.becomeCurrentWithPendingUnitCount(1)
try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: nil)
progress.resignCurrent()
XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
}
catch {
XCTFail()
}
}
func testQuickZip() {
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 fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTFail()
}
}
func testQuickZipFolder() {
do {
let fileManager = NSFileManager.defaultManager()
let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")!
let folderURL = NSBundle(forClass: ZipTests.self).bundleURL.URLByAppendingPathComponent("Directory")
let targetImageURL1 = folderURL.URLByAppendingPathComponent("3crBXeO.gif")
let targetImageURL2 = folderURL.URLByAppendingPathComponent("kYkLkPf.gif")
if fileManager.fileExistsAtPath(folderURL.path!) {
try fileManager.removeItemAtURL(folderURL)
}
try fileManager.createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil)
try fileManager.copyItemAtURL(imageURL1, toURL: targetImageURL1)
try fileManager.copyItemAtURL(imageURL2, toURL: targetImageURL2)
let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory")
XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
}
catch {
XCTFail()
}
}
func testZip() {
do {
let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
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
print(progress)
})
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!))
}
catch {
XCTFail()
}
}
func testZipUnzipPassword() {
do {
let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
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: "password", progress: { (progress) -> () in
print(progress)
})
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!))
guard let fileExtension = zipFilePath.pathExtension, let fileName = zipFilePath.lastPathComponent else {
throw ZipError.UnzipFail
}
let directoryName = fileName.stringByReplacingOccurrencesOfString(".\(fileExtension)", withString: "")
let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true)
try Zip.unzipFile(zipFilePath, destination: destinationUrl, overwrite: true, password: "password", progress: nil)
XCTAssertTrue(fileManager.fileExistsAtPath(destinationUrl.path!))
}
catch {
XCTFail()
}
}
func testQuickUnzipSubDir() {
do {
let bookURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
let unzipDestination = try Zip.quickUnzipFile(bookURL)
let fileManager = NSFileManager.defaultManager()
let subDir = unzipDestination.URLByAppendingPathComponent("subDir")
let imageURL = subDir.URLByAppendingPathComponent("r2W9yu9").URLByAppendingPathExtension("gif")
XCTAssertTrue(fileManager.fileExistsAtPath(unzipDestination.path!))
XCTAssertTrue(fileManager.fileExistsAtPath(subDir.path!))
XCTAssertTrue(fileManager.fileExistsAtPath(imageURL.path!))
} catch {
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)
}
}