iOS开发之文件解压缩库--SSZipArchive

本文介绍了iOS开发中使用SSZipArchive库进行文件解压缩的步骤。首先,从GitHub下载SSZipArchive并将其添加到项目中,还需链接库libz.tbd。接着,在XIB上创建按钮实现压缩和解压缩功能。虽然SSZipArchive没有解压缩的delegate响应,但其简单易用的特点使其成为移动端和Mac端的实用工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/chims-liu-touch/p/7100157.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值