打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(1)NSFileManager的简单使用
保存UIImage到文件
SSZipArchive使用详解
iPhone文件系统NSFileManager讲解
iOS开发
IOS成长之路
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服