美文网首页
iOS zip zap读取压缩文件

iOS zip zap读取压缩文件

作者: 冰宫无凉 | 来源:发表于2018-05-21 18:06 被阅读288次

在不解压zip文件的情况下肚去其中的文件,因为之前使用的ziparchive不能满足现在的需求

在终端输入:pod search zip zap,可以搜到当前的最新版本的(适应xcode的最新版本,若xcode不是最新的可以降低版本),然后添加进Podfile,再运行一下pod update --no-repo-update就可以添加到自己的工程当中了

在需要的地方添加#import<ZipZap/ZipZap.h>就可以了。

用法

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__);

            }else{

               return [UIImage imageWithData:data];

             }

         }

     }

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] - 1 - [[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;

 }

相关文章

  • iOS zip zap读取压缩文件

    在不解压zip文件的情况下肚去其中的文件,因为之前使用的ziparchive不能满足现在的需求 在终端输入:pod...

  • Linux基础04

    Linux压缩命令 .zip格式压缩 实例:压缩文件 zip 压缩文件名 原文件 实例:压缩文件夹 zip -r ...

  • Linux文件操作

    Linux命令格式 zip格式的压缩 zip 压缩文件名 源文件 压缩文件zip -r 压缩文件名 源文件压缩文...

  • 使用R优雅的处理压缩文件

    本节介绍在R中如何生成zip,tar.gz等格式的压缩文件,并批量读取压缩文件,废话不多说直接开干 安装并加载R包...

  • python zip 压缩 zipfile

    封装成函数如下: zip_add_dir添加目录到压缩文件zip_add_file添加文件到压缩文件zip_add...

  • rar vs zip优缺点对比

    zip格式的优点 zip的第一优点:普及率。 比如说,大部分在 internet 的压缩文件都是 zip 压缩文件...

  • 黑猴子的家:Linux zip/unzip 压缩解压缩命令

    1、zip 功能说明:压缩文件。语法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b ...

  • Python入门教程系列:zipfile

    ZIP是通用的归档和压缩格式。zipfile模块提供了通用的创建、读取、写入、附加和显示压缩文件的方法,你可以简单...

  • linux zip/unzip命令(转)

    命令名:zip 功能说明:压缩文件。 语法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][...

  • Mac 破解zip压缩文件密码详解

    使用fcrackzip来破解zip类型压缩文件 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具...

网友评论

      本文标题:iOS zip zap读取压缩文件

      本文链接:https://www.haomeiwen.com/subject/jkyzrttx.html