美文网首页OC-开发案例收集
SSZipArchive解压的基本使用

SSZipArchive解压的基本使用

作者: yyggzc521 | 来源:发表于2019-11-23 15:51 被阅读0次

    \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返回解压后文件路径

    相关文章

      网友评论

        本文标题:SSZipArchive解压的基本使用

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