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

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

概述本文将演示如何创建和解压一个包含密码的压缩包。 首先确保在项目中已经安装了所需的第三方库。 点击【Podfile】,查看安装配置文件。 1 platform :ios, ‘12.0‘2 use_frameworks!3 4 target ‘DemoApp‘ do5 source ‘https://github.com/CocoaPods/Specs.git‘6

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

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

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

1 platform :ios,12.02 use_frameworks!3 4 target ‘DemoApp do5     source https://github.com/CocoaPods/Specs.git6     pod Zip7 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         do20         {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) -> () in32                 //压缩的过程中,在控制台实时输出压缩的进度。33                 print(progress)34             })35             //输出压缩后的文件所在的@R_419_6128@36             print("destinationPath:\(documentsFolder)")37         }38         catch39         {40             print("Something went wrong")41         }42     }43     44     //解压加密压缩45     func unzipfileWithPassword()46     {47         //添加一个异常捕捉语句,实现解压加密压缩48         do49         {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) -> () in60                 //并在控制台输出解压进度61                 print(progress)62             })63             //输出解压后的文件所在的@R_419_6128@64             print("destinationPath:\(destinationPath)")65         }66         catch67         {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 }
总结

以上是内存溢出为你收集整理的[Swift通天遁地]七、数据与安全-(10)文件的加密压缩和解压加密压缩全部内容,希望文章能够帮你解决[Swift通天遁地]七、数据与安全-(10)文件的加密压缩和解压加密压缩所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存