iOS使用ZipArchive压缩文件

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

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

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

代码:

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

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

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

2011-04-08 09:42:19

Access数据库压缩文件

2009-12-25 17:55:05

Linux tar

2009-12-16 16:11:14

Linux压缩文件

2018-09-14 16:18:26

Linux压缩文件应用程序

2022-07-04 08:03:08

Go压缩打包

2015-08-07 10:02:34

Java线程池递归

2012-05-10 09:43:28

2020-11-08 14:37:46

Python压缩文件代码

2009-12-03 14:10:06

PHP压缩文件夹

2009-04-27 13:46:30

网络管理拷贝故障

2020-05-07 09:18:57

Linux压缩文件命令

2010-03-03 13:32:08

Python压缩文件

2019-04-26 09:50:21

Linux压缩文件文件夹

2011-09-15 10:12:11

ubunturar

2009-06-06 19:07:44

java压缩文件

2019-10-24 08:52:23

Linux压缩文件归档

2009-11-30 09:31:41

Windows 7文件解压

2019-02-26 13:39:55

Windows 10压缩文件夹密码保护

2023-06-01 09:38:27

微软WinRARWindows 11

2024-01-04 12:47:00

Linux命令工具
点赞
收藏

51CTO技术栈公众号