iOS zipzap读取压缩文件

时间:2023-03-08 19:26:36

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

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

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

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

iOS zipzap读取压缩文件

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

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

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

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

2、向zip包中添加内容

 ZZArchive* newArchive = [[ZZArchive alloc] initWithURL:[NSURL fileURLWithPath:@"/tmp/new.zip"] options: @{ZZOpenOptionsCreateIfMissingKey: @YES} error:nil];

 [newArchive updateEntries:
              @[
               [ZZArchiveEntry archiveEntryWithDirectoryName:@"folder/"],
              [ZZArchiveEntry archiveEntryWithFileName:@"folder/first.text"
               compress:YES
               dataBlock:^(NSError** error){
                                 return [@"hello, world" dataUsingEncoding:NSUTF8StringEncoding];
                                }]
              ]
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
 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)aimDirection{
NSError * error;
ZZArchive* archive = [ZZArchive archiveWithURL:[NSURL URLWithString:path] error:&error];
if (error) {
return NO;
} NSFileManager * fileManager = [NSFileManager defaultManager]; for (ZZArchiveEntry* entry in archive.entries)
{
if (!entry.fileMode || !S_IFDIR)
{
// Some archives don't have a separate entry for each directory
// and just include the directory's name in the filename.
// Make sure that directory exists before writing a file into it.
NSArray * arr = [entry.fileName componentsSeparatedByString:@"/"]; NSInteger index = [entry.fileName length] - - [[arr lastObject] length]; NSString * aimPath = [entry.fileName substringToIndex:index]; NSError * err;
[fileManager createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",aimDirection,aimPath] withIntermediateDirectories:YES attributes:nil error:&err];
if (err) {
return NO;
} NSData * data = [entry newDataWithError:nil];
[data writeToFile:[NSString stringWithFormat:@"%@/%@",aimDirection,entry.fileName] atomically:YES];
}
} [fileManager removeItemAtPath:path error:nil];
return YES;
}