分享

iOS 使用ZipArchive压缩文件

 叹落花 2015-01-30
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 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多