iOS 数据压缩与解压

iOS  数据压缩与解压


Hi,推荐文件给你 "数据压缩与解压.zip"


http://vdisk.weibo.com/s/Gbabp

http://pan.baidu.com/share/link?shareid=1658642783&uk=3189484501



本文中需要的第三库在本文的代码例子中可以下载。minizipZipArchive这两个第三库



ViewController.h代码如下:


#import <UIKit/UIKit.h>
#import "ZipArchive.h"
@interface ViewController : UIViewController
-(IBAction)compress:(id)sender;
-(IBAction)unAr:(id)sender;
@end



ViewController.m代码如下:

自己拖2个Button与如下两个方法相关联


//这方法中数据压缩的方法:

-(IBAction)compress:(id)sender
{
    //导入ZipArchive包之后要加入libz.1.2.5.dylib的库
    ZipArchive* zip = [[ZipArchive alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString* zipfilePath = [documentpath stringByAppendingString:@"/dj.zip"] ;
    //
    NSLog(@"%@",zipfilePath);
                     
                     
    NSString* image1 = [[NSBundle mainBundle] pathForResource:@"xiaonan" ofType:@"jpg"];
    NSString* image2 = [[NSBundle mainBundle] pathForResource:@"xiaonan" ofType:@"jpg"];
                     
                     
    //创建压缩包
    BOOL ret = [zip CreateZipFile2:zipfilePath];
    //向压缩包内加入数据
    ret = [zip addFileToZip:image1 newname:@"xiaonan.jpg"];
    ret = [zip addFileToZip:image2 newname:@"image2.jpg"];
    //关闭压缩包
    [zip CloseZipFile2];
                     
                     
    [zip release];
}




//数据解压的方法


-(IBAction)unAr:(id)sender
{
        
    ZipArchive* zip = [[ZipArchive alloc] init];
         
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
         
    NSString* l_zipfile = [documentpath stringByAppendingString:@"/dj.zip"] ;
    NSString* unzipto = [documentpath stringByAppendingString:@"/dj"] ;
         
         
    //找到需要解压文件的路径
    if( [zip UnzipOpenFile:l_zipfile] )
    {
        BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
        if( NO==ret )
        {
        }
        //完成解压
        [zip UnzipCloseFile];
    } 
    [zip release];
}


本文出自 “7087095” 博客,请务必保留此出处http://7097095.blog.51cto.com/7087095/1230891

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值