Objective-C zip 与 unzip

转自:http://willonboy.tk/?p=74

author website: http://www.flyblog.info/catprogramming/202.html
ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单.你只需要把代码加进你的工程,然后把zlib库添加到工程就可以了.
ziparchive可以在googlecode上下载
支持加密压缩与解压 代码在这里: ZLibDemo.zip (132.80 kb)
压缩:
ZipArchive可以压缩多个文件,只需要把文件一一addFileToZip即可.

ZipArchive* zip = [[ZipArchive alloc] init];
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dcoumentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* l_zipfile = [dcoumentpath stringByAppendingString:@"/test.zip"] ;
 
NSString* image1 = [dcoumentpath stringByAppendingString:@"/image1.jpg"] ;
NSString* image2 = [dcoumentpath stringByAppendingString:@"/image2.jpg"] ;
 
BOOL ret = [zip CreateZipFile2:l_zipfile];
ret = [zip addFileToZip:image1 newname:@"image1.jpg"];
ret = [zip addFileToZip:image2 newname:@"image2.jpg"];
if( ![zip CloseZipFile2] )
{
l_zipfile = @"";
}
[zip release];

解压缩:

ZipArchive* zip = [[ZipArchive alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dcoumentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* l_zipfile = [dcoumentpath stringByAppendingString:@"/test.zip"] ;
NSString* unzipto = [dcoumentpath stringByAppendingString:@"/test"] ;
if( [zip UnzipOpenFile:l_zipfile] ) {
  BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
  if( NO==ret ) { }
  [zip UnzipCloseFile];
}
[zip release];

转载请注明: 转自Rainbird的个人博客
   本文链接: Objective-C zip 与 unzip


相关博文

    分享到:

About rainbird

IOS攻城狮
This entry was posted in IOS开发 and tagged , , , . Bookmark the permalink.

发表评论