发布于 2017-05-25 03:24:47 | 96 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Swift教程,程序狗速度看过来!

Swift编程语言

SWIFT,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序。


本篇文章主要介绍了Swift实现文件压缩和解压示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

项目中有时候需要文件下载解压,项目中选择了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
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务