项目中有时候需要文件下载解压,项目中选择了ZipArchive,实际使用也比较简单,直接调用解压和压缩方法即可.
压缩
@IBAction func zipAction(_ sender: UIButton) {
let imageDataPath = Bundle.main.bundleURL.appendingPathComponent("FlyElephant").path
zipPath = tempZipPath()
let success = SSZipArchive.createZipFile(atPath: zipPath!, withContentsOfDirectory: imageDataPath)
if success {
print("压缩成功---\(zipPath!)")
}
}
#解压
@IBAction func unZipAction(_ sender: UIButton) {
guard let zipPath = self.zipPath else {
return
}
guard let unzipPath = tempUnzipPath() else {
return
}
let success = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
if !success {
return
}
print("解压成功---\(unzipPath)")
var items: [String]
do {
items = try FileManager.default.contentsOfDirectory(atPath: unzipPath)
} catch {
return
}
for (index, item) in items.enumerated() {
print("\(index)--文件名称---\(item)")
}
}
压缩和解压路径:
func tempZipPath() -> String {
var path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
path += "/\(UUID().uuidString).zip"
return path
}
func tempUnzipPath() -> String? {
var path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
path += "/\(UUID().uuidString)"
let url = URL(fileURLWithPath: path)
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return url.path
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html5模板网。
RxSwift学习教程之类型对象Subject详解这篇文章主要给大家介绍了关于RxSwift学习教程之类型对象Subject的相关资料,文中通过示例代码介绍的非常详细,对大
RxSwift学习之Observable的新建、订阅及取消订阅这篇文章主要给大家介绍了关于RxSwift学习教程之Observable的相关资料,文中详细的给大家介绍了关于新建Observable、订
RxSwift学习教程之基础篇RxSwift是Swift函数响应式编程的一个开源库,由Github的ReactiveX组织开发,维护。下面这篇文章主要给大家介绍了关于R
Swift4.0 Array数组详解这篇文章主要为大家详细介绍了Swift4.0 Array数组的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
利用Swift实现各类的CATransition动画详解CATransition动画主要在过渡时使用,比如两个页面层级改变的时候添加一个转场效果。CATransition分为两类,一类是公开
利用Swift如何判断iPhone X机型详解近日,iPhone X的发布在人们群众引起了很大的轰动,下面这篇文章主要给大家介绍了关于利用Swift如何判断iPhone X机型