iOS 使用ZipArchive压缩文件

01 +(NSString *)zipFiles:(NSArray *)paramFiles
02 {
03     //生成zip文件名字
04     NSString * zipFileName = [[CUtils generateRndString] stringByAppendingPathExtension:@"zip"];
05     //取得zip文件全路径
06     NSString * zipPath = [[CUtils documentPath] stringByAppendingPathComponent:zipFileName];
07
08     //判断文件是否存在,如果存在则删除文件
09     NSFileManager * fileManager = [NSFileManager defaultManager];
10     @try
11     {
12         if([fileManager fileExistsAtPath:zipPath])
13         {
14             if(![fileManager removeItemAtPath:zipPath error:nil])
15             {
16                 CCLog(@"Delete zip file failure.");
17             }
18         }
19     }
20     @catch (NSException * exception) {
21         CCLog(@"%@",exception);
22     }
23
24     //判断需要压缩的文件是否为空
25     if(paramFiles == nil || [paramFiles count] == 0)
26     {
27         CCLog(@"The files want zip is nil.");
28         return nil;
29     }
30
31     //实例化并创建zip文件
32     ZipArchive * zipArchive = [[ZipArchive alloc] init];
33     [zipArchive CreateZipFile2:zipPath];
34
35     //遍历文件
36     for(NSString * fileName in paramFiles)
37     {
38         NSString * filePath = [[CUtils documentPath] stringByAppendingPathComponent:fileName];
39         if([fileManager fileExistsAtPath:filePath])
40         {   //添加文件到压缩文件
41             [zipArchive addFileToZip:filePath newname:fileName];
42         }
43     }
44     //关闭文件
45     if([zipArchive CloseZipFile2])
46     {
47         CCLog(@"Create zip file success.");
48         [zipArchive release];
49         return zipPath;
50     }
51     [zipArchive release];
52     return nil;
53 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值