iOS - File Archive/UnArchive 文件压缩/解压
1、ZipArchive 方式
ZipArchive 只能对 zip 类文件进行压缩和解压缩
GitHub 网址:https://github.com/ZipArchive/ZipArchive
ZipArchive 使用 ARC
添加 ZipArchive
// 添加第三方库文件
ZipArchive // 包含系统动态库
libz.tbd (libz.dylib) // 包含头文件
#import "ZipArchive.h"
ZipArchive 压缩
文件压缩
// 目标路径
NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 源路径,待压缩的文件
NSArray *resourcesFilePaths = @[@"/Users/JHQ0228/Desktop/test1.rtf",
@"/Users/JHQ0228/Desktop/test2.rtf"]; BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
withFilesAtPaths:resourcesFilePaths];
文件夹压缩
// 目标路径
NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 源路径,待压缩的文件夹
NSString *resourcesDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
withContentsOfDirectory:resourcesDirPath];
ZipArchive 解压缩
普通解压
// 源路径,待解压缩的文件
NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
toDestination:destinationDirPath];
密码解压
// 源路径,待解压缩的文件
NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
toDestination:destinationDirPath
overwrite:YES
password:@"password"
error:nil];
协议解压
// 源路径,待解压缩的文件
NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
toDestination:destinationDirPath
delegate:self];
协议密码解压
// 源路径,待解压缩的文件
NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
toDestination:destinationDirPath
overwrite:YES
password:@"password"
error:NULL
delegate:self];
协议方法
- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total { NSLog(@"%f", (float)loaded/total);
}
2、SARUnArchiveANY 方式
SARUnArchiveANY 可以对 .zip, .rar, .7z 类文件进行压缩和解压缩。
SARUnArchiveANY 使用 ARC
添加 SARUnArchiveANY
// 添加第三方库文件
SARUnArchiveANY // 包含系统动态库
libz.tbd (libz.dylib) // 包含头文件
#import "SARUnArchiveANY.h"
#import "LZMAExtractor.h"
SARUnArchiveANY 解压缩
- (void)unZip {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example" ofType:@"zip"];
NSString *destPath = [self applicationDocumentsDirectory];
[self unArchive:filePath andPassword:nil destinationPath:destPath];
} - (void)unRar {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"rar"];
NSString *destPath = [self applicationDocumentsDirectory];
[self unArchive:filePath andPassword:nil destinationPath:destPath];
} - (void)unZip_pwd {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example_pwd" ofType:@"zip"];
NSString *destPath = [self applicationDocumentsDirectory];
[self unArchive:filePath andPassword:@"SARUnArchiveANY_ZIP" destinationPath:destPath];
} - (void)unRar_pwd {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example_pwd" ofType:@"rar"];
NSString *destPath = [self applicationDocumentsDirectory];
[self unArchive:filePath andPassword:@"SARUnArchiveANY_RAR" destinationPath:destPath];
} - (void)Unzip7z {
NSString *archiveFilename = @"example.7z";
NSString *archiveResPath = [[NSBundle mainBundle] pathForResource:archiveFilename ofType:nil];
NSAssert(archiveResPath, @"can't find .7z file");
NSString *destPath = [self applicationDocumentsDirectory];
[self unArchive:archiveResPath andPassword:nil destinationPath:destPath];
} - (void)unArchive: (NSString *)filePath andPassword:(NSString*)password destinationPath:(NSString *)destPath { NSAssert(filePath, @"can't find filePath"); SARUnArchiveANY *unarchive = [[SARUnArchiveANY alloc] initWithPath:filePath]; if (password != nil && password.length > 0) {
unarchive.password = password;
} if (destPath != nil) { // (Optional). If it is not given, then the file is unarchived in the same location of its archive/file.
unarchive.destinationPath = destPath;
} unarchive.completionBlock = ^(NSArray *filePaths) { NSLog(@"For Archive : %@", filePath); if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"US Presidents://"]]) { NSLog(@"US Presidents app is installed.");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"US Presidents://"]];
} for (NSString *filename in filePaths) {
NSLog(@"File: %@", filename);
}
}; unarchive.failureBlock = ^(){
NSLog(@"Cannot be unarchived");
}; [unarchive decompress];
} - (void)handleFileFromURL:(NSString *)filePath {
NSLog(@"********* FILES FROM THE OTHER APPS *********");
[self unArchive:filePath andPassword:nil destinationPath:nil];
} - (NSString *) applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
iOS - File Archive/UnArchive 文件压缩/解压的更多相关文章
- Linux 之 文件压缩解压
文件压缩解压 参考教程:[千峰教育] 命令: gzip: 作用:压缩文件,只能是单个文件,不能是多个,也不能是目录. 格式:gzip file 说明:执行命令会生成file.gz,删除原来的file ...
- linux驱动系列之文件压缩解压小节(转)
转至网页:http://www.jb51.net/LINUXjishu/43356.html Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通 ...
- 分享一个ASP.NET 文件压缩解压类 C#
需要引用一个ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; ...
- linux文件压缩解压命令
01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...
- Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...
- linux命令:压缩解压命令
压缩解压命令:gzip 命令名称:gzip 命令英文原意:GNU zip 命令所在路径:/bin/gzip 执行权限:所有用户 语法:gzip 选项 [文件] 功能描述:压缩文件 压缩后文件格式:g ...
- c#自带压缩类实现的多文件压缩和解压
用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容.下面的代码没有把多文件的目录 ...
- 对称加密之AES、压缩解压以及压缩加密解密解压综合实战
AES 压缩解压 压缩加密解密解压 对称加密: 就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密.密钥是控制加密及解密过程的指令.算法是一组规则,规定如何进行加密和解密. 因此加密的安 ...
- linux笔记:linux常用命令-压缩解压命令
压缩解压命令:gzip(压缩文件,不保留原文件.这个命令不能压缩目录) 压缩解压命令:gunzip(解压.gz的压缩文件) 压缩解压命令:tar(打包压缩目录或者解压压缩文件.打包的意思是把目录打包成 ...
随机推荐
- 在Linux下搭建Git服务器的方法是什么样?
第一步 安装git:可以通过命令的方式快速安装,不同的linux的安装方法可能不一样,我的是采用的yum方法.ubuntu可以用apt-get命令.sudo yum install git 第二步 添 ...
- CentOS下使用Percona XtraBackup对MySQL5.6数据库innodb和myisam的方法
Mysql卸载从下往上顺序 [root@localhost /]# rpm -e --nodeps qt-mysql-4.6.2-26.el6_4.x86_64[root@localhost /]# ...
- jquery中的节点的操作
节点的操作 Dom 文档对象 模型 解决 一.插入节点 Append() 在每个匹配的元素中追加内容 Var $li_1= “<li></li>”; Var $li_2 = ...
- LeetCode----67. Add Binary(java)
package addBinary67;/* Given two binary strings, return their sum (also a binary string).For example ...
- http协议了解
在web应用中,服务器把网页的HTML代码发送给浏览器,让浏览器显示出来,浏览器和服务器之间的传输协议就是HTTP协议.HTTP是在网络上传输HTML的协议,用于浏览器和服务器之间的通信. 一个网页打 ...
- Python 字典(Dictionary)
字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...
- [团队项目]----Math Calculator
团队项目 ----Math Calculator 任务: 1.每个团队从Github上fork这个项目的源代码 https://github.com/RABITBABY/We-have-bing 2. ...
- jquery闭包的使用
<div id="divTest"> Test </div> <br /> <hr /> <div id="divT ...
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
描述 Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute ...
- JAVAWeb使用POI做导出Excel
一.需要了解的API ①HSSFWorkBook //代表一个Excel文件 ②HSSFSheet //代表一个表 ③HSSFRow //代表一个表中的某一行 ④HSSFCell //代表一个表中的某 ...