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

 

posted @ 2017-07-01 10:48  象棋中的象棋  阅读(689)  评论(0编辑  收藏  举报