iOS zipzap读取压缩文件

摘要:
最近在公司遇到一项需求,在不解压zip文件的情况下读取其中的文件,因为之前使用的ziparchive不能满足现在的需求,所以在网上一阵狂搜,终于找到了zipzap,实话说还真的难找。之前读取本地zip文件的内容时会把它解压出来,运行app之后会发现原来的zip文件被同名文件夹取代,使用zipzap之后便不会再有这种情况,另外自己还可以根据需要把读取的文件写入某个文件夹。

最近在公司遇到一项需求,在不解压zip文件的情况下读取其中的文件,因为之前使用的ziparchive不能满足现在的需求,所以在网上一阵狂搜,终于找到了zipzap,实话说还真的难找。

之前读取本地zip文件的内容时会把它解压出来,运行app之后会发现原来的zip文件被同名文件夹取代,使用zipzap之后便不会再有这种情况,另外自己还可以根据需要把读取的文件写入某个文件夹。

只需要在终端输入:pod search zipzap ,回车就可以找到它的当前版本,然后添加进Podfile,再运行一下pod update --no-repo-update就可以把它添加进自己的工程了。

在需要使用的地方添加#import <ZipZap/ZipZap.h> 就可以使用了,是不是很方便?

iOS zipzap读取压缩文件第1张

下面介绍一下zipzap的四种使用场景:

1、从zip中读取已知内容[即你知道所需要文件的文件名]

//把zip数据包转换成ZZArchive对象
1 ZZArchive *archive = [ZZArchive archiveWithURL:[NSURL fileURLWithPath:zip] error:&error];
//zip中的数据都在archive 的 [NSArrayentries属性中,
//所以可以通过for循环,或者NSArray自带的方法遍历entries中的元素获取自己想要的结果
//通常情况下我们只知道所需资源的名字,这样就可以使用下面的代码来实现
1     for (ZZArchiveEntry * ectry inarchive.entries) {
2         if([ectry.fileName isEqualToString:name]) { 
3             NSData * data = [ectry newDataWithError:&error]; 
4             if(error) { 
5 //NSLog(@"--------data error:%@--------
 %s",error,__FUNCTION__);
 6             }else{ 
 7                 return[UIImage imageWithData:data];
 8 } 
 9 } 
10     }

//上面的代码只是从一个zip包中取出对应的图片,如果zip中的文件有一定的目录结构,大家可以打印一下ectry.fileName看看是什么样子,看过之后就会明白了

2、向zip包中添加内容

1 ZZArchive* newArchive = [[ZZArchive alloc] initWithURL:[NSURL fileURLWithPath:@"/tmp/new.zip"] options: @{ZZOpenOptionsCreateIfMissingKey: @YES} error:nil];
2 
3 [newArchive updateEntries:
4 @[
5                   [ZZArchiveEntry archiveEntryWithDirectoryName:@"folder/"],
6                   [ZZArchiveEntry archiveEntryWithFileName:@"folder/first.text"
7 compress:YES
8                                                  dataBlock:^(NSError**error){
9                                          return [@"hello, world"dataUsingEncoding:NSUTF8StringEncoding];
10 }]
11 ]
12 error:nil];

3、更新zip包中的内容

ZZArchive* archive = [ZZArchive archiveWithURL:[NSURL fileURLWithPath:@"/tmp/old.zip"] error:nil];
NSMutableArray* entries =[archive.entries mutableCopy];
[entries replaceObjectAtIndex:replacingIndex
                   withObject: [ZZArchiveEntry archiveEntryWithFileName:@"replacement.text"compress:YES
                                                              dataBlock:^(NSError**error){
                                                                    return [@"see you again, world"dataUsingEncoding
                                                                }]];
[archive updateEntries:entries error:nil];

4、解压zip包

//path : zip路径
//aimDirection : 解压到什么位置
//如果解压成功,则返回YES,否则返回NO
1 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)aimDirection{
2     NSError *error;
3     ZZArchive* archive = [ZZArchive archiveWithURL:[NSURL URLWithString:path] error:&error];
4     if(error) {
5         returnNO;
6 }
7     
8     NSFileManager * fileManager =[NSFileManager defaultManager];
9     
10     for (ZZArchiveEntry* entry inarchive.entries)
11 {
12         if (!entry.fileMode || !S_IFDIR)
13 {
14             //Some archives don't have a separate entry for each directory
15             //and just include the directory's name in the filename.
16             //Make sure that directory exists before writing a file into it.
17             NSArray * arr = [entry.fileName componentsSeparatedByString:@"/"];
18             
19             NSInteger index = [entry.fileName length] - 1 -[[arr lastObject] length];
20             
21             NSString * aimPath =[entry.fileName substringToIndex:index];
22             
23             NSError *err;
24             [fileManager createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",aimDirection,aimPath] withIntermediateDirectories:YES attributes:nil error:&err];
25             if(err) {
26                 returnNO;
27 }
28             
29             NSData * data =[entry newDataWithError:nil];
30             [data writeToFile:[NSString stringWithFormat:@"%@/%@",aimDirection,entry.fileName] atomically:YES];
31 }
32 }
33     
34 [fileManager removeItemAtPath:path error:nil];
35     returnYES;
36 }

免责声明:文章转载自《iOS zipzap读取压缩文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇RDLC报表系列四lua 模块与包(五)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python安装官方whl包、tar.gz包、zip包

python依赖包下载官方地址:https://pypi.org/ 一、whl包安装   pip3 install XlsxWriter-1.0.5-py2.py3-none-any.whl  二、tar.gz包安装   安装tar.gz,命令格式:tar  -zxvf   压缩文件名.tar.gz解压后,cd文件 ->./configure  -&...

Mac上zip,rar,tar文件命令解压和压缩

经常遇到在windowns上的压缩文件,在mac上解压出现问题,特意总结了下在Terminal里常用命令的方式解压和压缩文件 1、zip压缩文件 zip命令的参数很多,可以利用"zip --help"查看,在这里就不在一一说明了,下面只说几个常用的 zip -q -r -e -m -o 'yourName.zip' "zipfile list''-q :不...

从HTTP地址下载.zip文件 解析GZIP文件

1 packagecom.example.zipjiexitest; 2 3 importjava.io.BufferedInputStream; 4 importjava.io.DataInputStream; 5 importjava.io.File; 6 importjava.io.FileInputStream; 7 importjava.io...

出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功

出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功,原因是由于在安装php的时候没有增加php zip的支持(非zlib)。在Windows下的解决办法是:1、在php.ini文件中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip...

攻防世界-crypto-你猜猜(常用文件的文件头、Ziperello爆破压缩包密码)

题目来源: ISCC-2017题目描述:我们刚刚拦截了,敌军的文件传输获取一份机密文件,请君速速破解。 下载附件后,内容如下: 504B03040A0001080000626D0A49F4B5091F1E0000001200000008000000666C61672E7478746C9F170D35D0A45826A03E161FB96870EDDFC7C...

Linux下添加php的zip模块

./configure --with-php-config=/usr/local/php/bin/php-config extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/zip.so Linux下添加php的zip模块 今天早上开发的人员过来跟我说,测试机上的XX项目...