SSZipArchive解压的基本使用

\color{red}{对文件进行压缩}

- (void)zip {
    NSArray *arrayM = @[
                        @"/Users/xiaomage/Desktop/Snip20160226_2.png",
                        @"/Users/xiaomage/Desktop/Snip20160226_6.png"
                        ];
    /*
     第一个参数:压缩文件的存放位置
     第二个参数:要压缩哪些文件(路径)
     */
    //[SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/Test.zip" withFilesAtPaths:arrayM];
    [SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/Test.zip" withFilesAtPaths:arrayM withPassword:@"123456"];
}

\color{red}{对文件夹进行压缩}

- (void)zip2 {
    /*
     第一个参数:压缩文件存放位置
     第二个参数:要压缩的文件夹(目录)
     */
    [SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" withContentsOfDirectory:@"/Users/xiaomage/Desktop/demo"];
}

\color{red}{对压缩包(可能是文件也可能是文件夹)进行解压缩}

- (void)unzip {
    /*
     第一个参数:要解压的文件在哪里
     第二个参数:文件应该解压到什么地方
     */
    //[SSZipArchive unzipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" toDestination:@"/Users/xiaomage/Desktop/xx"];
    
    [SSZipArchive unzipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" toDestination:@"/Users/xiaomage/Desktop/xx" progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
        // entryNumber 代表正在解压缩第几个文件
        // total 代表总共有多少个文件需要解压缩
        // 该block会调用多次,在这里可以获取解压缩的进度
        NSLog(@"%zd---%zd",entryNumber,total);
        
    } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
        
        NSLog(@"%@",path);
    }];
}

解压是异步的:
为什么要异步,ios游戏类项目中,如果解压在ui主线程,会占用进程,效果是导致app卡在启动图界面,解压完成后才能继续运行。异步解压,这样用户体验会很好。

\color{red}{获取解压后文件列表}

/**
 SSZipArchive解压

 @param path 压缩包文件路径
 */
- (void)uSSZipArchiveWithFilePath:(NSString *)path {
    //Caches路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    //解压目标路径
    NSString *destinationPath =[cachesPath stringByAppendingPathComponent:@"SSZipArchive"];
   
    //解压
    BOOL isSuccess = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath];
    
    //如果解压成功则获取解压后文件列表
    if (isSuccess) {
        [self obtainZipSubsetWithFilePath:destinationPath];
    }
    
}


/**
 获取解压后文件列表

 @param path 解压后的文件路径
 */
- (void)obtainZipSubsetWithFilePath:(NSString *)path {
    NSString *destinationPath =[path stringByAppendingPathComponent:@"压缩包名(不需要后缀)"];
    // 读取文件夹内容
    NSError *error = nil;
    NSMutableArray*items = [[[NSFileManager defaultManager]
                             contentsOfDirectoryAtPath:destinationPath
                             error:&error] mutableCopy];
    if (error) {
        return;
    }
    
    for (NSString * item_str in items) {
        NSLog(@"文件名:%@",item_str);
    }
}

zip解压缩第三方类库ZipArchive返回解压后文件路径

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我希望 有一天 夕阳西下时 端着咖啡 听着民谣 斜靠藤椅 待在自己的小店里 问候门外来往的人 之后便放空 花很长的...
    赤桦与执阅读 92评论 0 0
  • 豪豪_983b阅读 239评论 0 0
  • 管宁,一个清高的居士,有着横溢的才华但没有入仕为官,他不与世俗同流,一心只做自己的隐士,不管是功名利禄还是前途无量...
    小虎007阅读 822评论 1 1