IOS开发之——文件的压缩和解压缩

一 概述

  • 第三方解压缩框架——SSZipArchive
  • 文件压缩示例
  • 文件解压缩示例

二 第三方解压缩框架——SSZipArchive

2.1 项目地址

Github/ZipArchive:https://github.com/ZipArchive/ZipArchive

2.2 添加SSZipArchive依赖

在项目位置打开终端,执行如下指令

pod init

打开Podfile文件,添加SSZipArchive依赖

pod 'SSZipArchive'

添加依赖更新

pod install

2.3 SSZipArchive使用

在引用位置添加依赖头文件

#import <SSZipArchive.h>

创建Zip压缩

[SSZipArchive createZipFileAtPath:zipPath withContentsOfDirectory:sampleDataPath];

Zip解压缩

[SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath];

三 文件压缩示例

3.1 代码

-(void)createZip
{
    NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *images=[caches stringByAppendingPathComponent:@"images"];
    NSString *zipFile=[caches stringByAppendingPathComponent:@"images.zip"];
    
    //创建一个zip文件压缩
    [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];
}

3.2 效果图(将缓存目录下的images压缩)

四 文件解压缩示例

4.1 代码

-(void)unZip
{
    NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *unzipPath=[caches stringByAppendingPathComponent:@"images.zip"];
    NSString *desPath=[caches stringByAppendingPathComponent:@"unzip"];
    
    [SSZipArchive unzipFileAtPath:unzipPath toDestination:desPath];
}

4.2 效果图(将缓存目录下的zip解压缩)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值