为有牺牲多壮志,敢教日月换新天。

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

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

目录:[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 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 }

 

posted @ 2019-01-24 12:40  为敢技术  阅读(564)  评论(0编辑  收藏  举报