Zip使用

Zip使用

========================

感觉官方写得挺明白的

1
2
3
4
5
6
7
8
9
do {
let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
let unzipDirectory = try Zip.quickUnzipFile(filePath) // Unzip
let zipFilePath = try Zip.quickZipFiles([filePath], fileName: "archive") // Zip
}
catch {
print("Something went wrong")
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do {
let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
let documentsDirectory = FileManager.default.urls(for:.documentDirectory, in: .userDomainMask)[0]
try Zip.unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
print(progress)
}) // Unzip

let zipFilePath = documentsFolder.appendingPathComponent("archive.zip")
try Zip.zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
print(progress)
}) //Zip

}
catch {
print("Something went wrong")
}
1
Zip.addCustomFileExtension("file-extension-here")

我是这样用的

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
func unzipModels(_ path: [String], modelUrl: String, directory: String) {
for item in path {
let fileManager = FileManager.default
let urls: [URL] = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let documentURL = urls.first!
let url = documentURL.appendingPathComponent(directory + item)
let urlForTemp = documentURL.appendingPathComponent("Temp", isDirectory: true)
let destinationURL = documentURL.appendingPathComponent(directory + "File", isDirectory: true)
while true {
if fileManager.fileExists(atPath: url.path) {
break
} else {
Thread.sleep(forTimeInterval: 1)
continue
}
}
do {
try Zip.unzipFile(url, destination: urlForTemp, overwrite: true, password: nil, progress: { (progress) in
if progress == 1 {
do {
if fileManager.fileExists(atPath: destinationURL.path ) {
try fileManager.removeItem(atPath: destinationURL.path)
}
try fileManager.moveItem(atPath: urlForTemp.path, toPath: destinationURL.path)
try fileManager.removeItem(atPath: url.path)
} catch {
log(error)
}
}
})
} catch {
log(error)
}
}
}
1
2
3
4
5
6
func downloadModelsAndUnzip(_ path: [String], modelUrl: String, directory: String) {
DispatchQueue.global().async {
downloadModelsWithoutDispatchQueue(path, modelUrl: modelUrl, directory: directory)
unzipModels(path, modelUrl: modelUrl, directory: directory)
}
}