加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift通天遁地]七、数据与安全-(10)文件的加密压缩和解压加密压

发布时间:2020-12-14 05:05:23 所属栏目:百科 来源:网络整理
导读:本文将演示如何创建和解压一个包含密码的压缩包。 首先确保在项目中已经安装了所需的第三方库。 点击【Podfile】,查看安装配置文件。 1 platform :ios, ‘ 12.0 ‘ 2 use_frameworks! 3 4 target ‘DemoApp ‘ do 5 source ‘ https://github.com/CocoaPods/

本文将演示如何创建和解压一个包含密码的压缩包。

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

点击【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 import Zip
 3 
 4 class ViewController: UIViewController {
 5     
 6     override func viewDidLoad() {
 7         super.viewDidLoad()
 8         // Do any additional setup after loading the view,typically from a nib.
 9         //加密压缩
10         zipFileWithPassword()
11         //解压加密压缩
12         unzipFileWithPassword()
13     }
14     
15     //加密压缩
16     func zipFileWithPassword()
17     {
18         //添加一个异常捕捉语句,实现压缩文件
19         do
20         {
21             //初始化一个字符串常量,表示项目中带压缩文件的路径
22             let filePath = Bundle.main.url(forResource: "BankAndCity",withExtension: "sqlite")!
23             //获得沙箱目录中,文档文件的路径
24             var documentsFolder = FileManager.default.urls(for:.documentDirectory,in: .userDomainMask)[0]
25             //在文档文件路径的末尾,添加文件的名称,作为压缩后的文件所在的路径。
26             documentsFolder = documentsFolder.appendingPathComponent("NewArchivedFile.zip")
27             //调用第三方类库的压缩文件的方法,将数据库文件进行压缩,并设置安全密码。
28             //同时设置压缩的模式为最佳模式.
29             try Zip.zipFiles(paths: [filePath],zipFilePath: documentsFolder,password: "coolketang",compression: .BestCompression,progress:
30             { 
31                 (progress) -> () in
32                 //压缩的过程中,在控制台实时输出压缩的进度。
33                 print(progress)
34             })
35             //输出压缩后的文件所在的绝对路径
36             print("destinationPath:(documentsFolder)")
37         }
38         catch
39         {
40             print("Something went wrong")
41         }
42     }
43     
44     //解压加密压缩
45     func unzipFileWithPassword()
46     {
47         //添加一个异常捕捉语句,实现解压加密压缩
48         do
49         {
50             //获得在沙箱目录中,文档文件夹的路径
51             var documentsFolder = FileManager.default.urls(for:.documentDirectory,in: .userDomainMask)[0]
52             //在文档文件路径的末尾,添加文件的名称,作为在上一个方法中压缩文件所在的位置
53             documentsFolder = documentsFolder.appendingPathComponent("NewArchivedFile.zip")
54             let documentsDirectory = NSHomeDirectory() + "/Documents/"
55             //初始化一个网址对象,作为解压后的文件的目标位置。
56             let destinationPath = URL(fileURLWithPath: documentsDirectory)
57             //调用第三方类库的解压文件的方法,设置解压的密码,
58             //将指定的压缩文件,解压到指定的文件夹。
59             try Zip.unzipFile(documentsFolder,destination: destinationPath,overwrite: true,password: "coolketang",progress: { (progress) -> () in
60                 //并在控制台输出解压进度
61                 print(progress)
62             })
63             //输出解压后的文件所在的绝对路径
64             print("destinationPath:(destinationPath)")
65         }
66         catch
67         {
68             print("Something went wrong")
69         }
70     }
71     
72     override func didReceiveMemoryWarning() {
73         super.didReceiveMemoryWarning()
74         // Dispose of any resources that can be recreated.
75     }
76 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读