iOS使用ZipArchive压缩文件

移动开发 iOS
今天开发应用需要使用到压缩文件功能,在网上查找了一下相关资料,发现ZipArchive使用相对简单点,自己就写了个demo分享给大家。

今天开发应用需要使用到压缩文件功能,在网上查找了一下相关资料,发现ZipArchive使用相对简单点,自己就写了个demo函数:

ZipArchive下载地址:https://code.google.com/p/ziparchive/

代码:

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

ps:代码里面使用的CCLog是自定义

责任编辑:闫佳明 来源: oschina
相关推荐

2011-04-08 09:42:19

Access数据库压缩文件

2009-12-16 16:11:14

Linux压缩文件

2009-12-25 17:55:05

Linux tar

2022-07-04 08:03:08

Go压缩打包

2018-09-14 16:18:26

Linux压缩文件应用程序

2012-05-10 09:43:28

2024-05-23 11:33:42

python代码开发

2015-08-07 10:02:34

Java线程池递归

2010-03-03 13:32:08

Python压缩文件

2020-05-07 09:18:57

Linux压缩文件命令

2009-04-27 13:46:30

网络管理拷贝故障

2009-12-03 14:10:06

PHP压缩文件夹

2020-11-08 14:37:46

Python压缩文件代码

2009-06-06 19:07:44

java压缩文件

2011-09-15 10:12:11

ubunturar

2019-04-26 09:50:21

Linux压缩文件文件夹

2023-06-01 09:38:27

微软WinRARWindows 11

2024-01-04 12:47:00

Linux命令工具

2019-10-24 08:52:23

Linux压缩文件归档

2019-02-26 13:39:55

Windows 10压缩文件夹密码保护
点赞
收藏

51CTO技术栈公众号