- 下载SSZipArchive库(新版本可能没有createZipFileAtPath方法)
-
导入依赖动态库libz
压缩目录下的所有文件(SSZipArchive.createZipFileAtPath)
@IBAction func Compression(sender: UIButton) {
let caches = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last
let images = caches?.stringByAppendingString("/images")
// 创建一个zip文件
let zipFile = caches?.stringByAppendingString("/images.zip")
// 把images目录压缩成zipFile文件
let resule = SSZipArchive.createZipFileAtPath(zipFile, withContentsOfDirectory: images)
if resule {
/*********************把压缩文件上传到服务器*******************/
// 非文件参数
let params = [
"username" : "李四"
]
let mimeType = Tool.MIMEType(zipFile!)
let data = NSData(contentsOfFile: zipFile!)
Tool.upload(url, fileName: "images.zip", mimeType: mimeType, fileData: data!, params: params) { (response, data, error) -> () in
// 得到返回的json
let dict = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves)
print(dict)
}
/*********************把压缩文件上传到服务器*******************/
}
}
解压缩从服务器下载的zip文件
@IBAction func Extract(sender: UIButton) {
// 服务器上zip文件地址
let url = NSURL(string: "http://localhost:8080/MJServer/resources/videos/videos.zip")
// 发送请求,下载文件
let task = NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location, response, error) -> Void in
// 拿到沙盒caches路径
let caches = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last
// 解压缩zip文件
SSZipArchive.unzipFileAtPath(location?.path, toDestination: caches)
}
// 开始下载
task.resume()
}
网友评论