iOS中压缩和解压缩文件--ZipArchive


    之前在做项目的时候,从网络请求的数据有可能是压缩文件,如何解压呢,这个是个问题,网上看了一看有很多,我自己就找了一个感觉不错的第三方,使用的时候也简单,就来试试吧。使用ZipArchive来进行iOS的压缩和解压缩文件

    我用的第三方文件是ZipArchive,这个是大家经常用到的。


将第三方倒入到工程中就可以

调用头文件

#import "ZipArchive.h"


首先是压缩文件


- (void)zipFunction
{
    zip = [[ZipArchive alloc] init];
    NSString *documentPath = [self documentsPath];
    NSString * zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
    
    NSString * image1 = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg" inDirectory:nil];
    NSString * image2 = [[NSBundle mainBundle] pathForResource:@"b" ofType:@"jpg" inDirectory:nil];
    BOOL result = [zip CreateZipFile2:zipFile];
    result = [zip addFileToZip:image1 newname:@"a.jpg"];
    result = [zip addFileToZip:image2 newname:@"b.jpg"];
    if( ![zip CloseZipFile2] ){
        zipFile = @"";
    }
    [zip release];
    NSLog(@"%@",NSHomeDirectory());
}

然后解压函数

- (void)unzip
{
    zip = [[ZipArchive alloc] init];
    NSString *documentPath = [self documentsPath];
    NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
    NSString* unZipTo = [documentPath stringByAppendingString:@"/images"] ;
    if( [zip UnzipOpenFile:zipFile] ){
        BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
        if( NO==result ){
            //添加代码
        }
        [zip UnzipCloseFile];

        NSString * imageField = [unZipTo stringByAppendingPathComponent:@"a.jpg"];
        NSData * imagedata = [NSData dataWithContentsOfFile:imageField];
        UIImage * image = [UIImage imageWithData:imagedata];
        
        UIImageView * iamgePic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        
        [iamgePic setImage:image];
        
        [self.view addSubview:iamgePic];

    }
    [zip release];
}

这样就把a图片解压出来了

很简单吧。。。。。

剩下的就是自己调用了。。。。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值