zoukankan      html  css  js  c++  java
  • iOS开发之文件解压缩库--SSZipArchive

    SSZipArchive的下载路径:   https://github.com/ZipArchive/ZipArchive

    SSZipArchive是github上大神Sam Soffes的一个基于C编写的开源项目,然后扩展在OC上使用,支持移动端及Mac端.

    SSZipArchive的使用其实比较简单,首先创建一个Demo项目,并将框架拖入到当前项目,并在"Linked Frameworks and libraries"项Add库libz.tbd,不然运行会报错误.

    在XIB上创建两个UIButton分别用于进行解压缩操作,代码如下:

    1.压缩打包文本为zip文件---------遗憾的是没有delegate响应

    - (IBAction)getZip:(id)sender {
              //获取沙盒document文件夹路径
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
              //设置压缩后的文件路径
        NSString *zipPath = [documentPath stringByAppendingPathComponent:@"destion.zip"];
              //原文件路径
        NSString *sourcePath = [[NSBundle mainBundle]pathForResource:@"README" ofType:@"txt"];
       
        if ([SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:@[sourcePath]]) {
            NSLog(@"zip success");
        }
    }
    View Code

    2.解压

    - (IBAction)unZip:(id)sender {
           //获取沙盒document文件夹路径
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
             // 解压文件路径
        NSString *sourcePath = [documentPath stringByAppendingPathComponent:@"destion.zip"];
            //解压目标文件夹路径
        NSString *unZipPath = [documentPath stringByAppendingPathComponent:@"docunment"];
        NSLog(@"unZipPath = %@",unZipPath);
            //解压,使用delegate获取unzip info
        BOOL isUnZip = [SSZipArchive unzipFileAtPath:sourcePath toDestination:unZipPath delegate:self];
        if (!isUnZip) {
            NSLog(@"unzip fail!");
        }
    }
    View Code

     3.代理

    #pragma   mark ---      SSZipArchiveDelegate
    - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo{
        //最先执行,表示准备解压
        NSLog(@"Will..........%@........%ld",path,zipInfo.size_comment);
    }
    
    - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath{
       ////// 解压完成时候执行
        NSLog(@"%s",__func__);
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *unZipPath = [documentPath stringByAppendingPathComponent:@"docunment"];
        NSArray *fileArr = [[NSFileManager defaultManager] subpathsAtPath:unZipPath];
        for (NSString *str in fileArr) {
            NSLog(@"%@",str);
        }
    }
    
    - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total{
        ///// 解压进度
            NSLog(@"%s.........%lld............%lld",__FUNCTION__,loaded,total);
    
    }
    View Code
    提高技能如同提升自信心。
  • 相关阅读:
    记一次java程序内存溢出问题
    js 对象数据观察者实现
    requirejs和seajs使用感受
    maven根据不同的运行环境,打包不同的配置文件
    Quartz .net 一直运行失败
    Sql2008R2 日志无法收缩解决方案
    win7 64位英文版 ado驱动
    KB4284826 远程桌面发生身份验证错误,要求的函数不受支持
    Delphi System.zip patch with ZIP64 and LZMA supports
    native excel 文件已经打开的判断
  • 原文地址:https://www.cnblogs.com/chims-liu-touch/p/7100157.html
Copyright © 2011-2022 走看看