★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10313735.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

目录:[Swift]通天遁地Swift

本文将演示使用开源类库实现文件的压缩和解压操作。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target ‘DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'Zip'
7 end

根据配置文件中的相关配置,安装第三方库。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 //引入已经安装的第三方类库
 3 import Zip
 4 
 5 class ViewController: UIViewController {
 6     
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view, typically from a nib.
10         
11         //添加一个异常捕捉语句,实现文件的解压和压缩
12         do
13         {
14             //初始化一个字符串常量,表示项目中压缩文件的路径
15             let filePath = Bundle.main.url(forResource: "BankAndCity.sqlite", withExtension: "zip")!
16             
17             //调用第三方类库的解压方法,对指定路径的压缩文件进行解压操作
18             let unzipDirectory = try Zip.quickUnzipFile(filePath)
19             //在控制台输出解压后的文件路径
20             print("unzipDirectory:\(unzipDirectory)")
21             
22             //调用第三方类库的压缩文件的方法,可以将多个文件,合并为一个压缩包,并设置压缩后的文件名称。
23             let zipFilePath = try Zip.quickZipFiles([unzipDirectory], fileName: "archivedFile")
24             //在控制台输出压缩后的文件路径
25             print("zipFilePath:\(zipFilePath)")
26         }
27         catch
28         {
29             print("Something went wrong")
30         }
31     }
32     
33     override func didReceiveMemoryWarning() {
34         super.didReceiveMemoryWarning()
35         // Dispose of any resources that can be recreated.
36     }
37 }

 

转载于:https://www.cnblogs.com/strengthen/p/10313735.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐