ZipArchive解压文件

一、首先介绍minizip 的使用方法

ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单
方法:从http://code.google.com/p/ziparchive/ 上下载ZipArchive.zip,解压后将代码加入工程中,并且把zlib库添加到工程中
使用方法:
1. 压缩:ZipArchive可以压缩多个文件,只需要把文件一一addFileToZip即可.

[cpp] view plaincopy
 

ZipArchive* zip = [[ZipArchive alloc] init];  
NSArray *paths = NSSearchPathForDirectoriesInDomainsNSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentpath = [paths count] > 0) ? [paths objectAtIndex:0] : nil;  
NSString* l_zipfile = [documentpath stringByAppendingString:@“/test.zip”] ;  
   
NSString* image1 = [documentpath stringByAppendingString:@“/image1.jpg”] ;  
NSString* image2 = [documentpath 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];  

 

2. 解压缩:

[cpp] view plaincopy
 

ZipArchive* zip = [[ZipArchive alloc] init];  
NSArray *paths = NSSearchPathForDirectoriesInDomainsNSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentpath = [paths count] > 0) ? [paths objectAtIndex:0] : nil;  
   
NSString* l_zipfile = [documentpath stringByAppendingString:@“/test.zip”] ;  
NSString* unzipto = [documentpath stringByAppendingString:@“/test”] ;  
if [zip UnzipOpenFile:l_zipfile] )  
 {  
   BOOLret = [zip UnzipFileTo:unzipto overWrite:YES];  
   if NO==ret )  
   {  
   }  
   [zip UnzipCloseFile];  
 }  
[zip release];  

 

以上摘自:http://www.cnblogs.com/a7345678/archive/2012/06/27/2566125.html 暗夜精灵-鬼才阁。 

二、压缩包含中文的文件时,到windows下解压后出现乱码。

这个问题原因是Ios版本的ZipArchive工程中,编码格式变为UTF-8.然后windows上的编码格式多数是GBK。

那么打开ZipArchive的源码,改变编码方式就行了。

找到函数:

-BOOL) addFileToZip:NSString*) file newname:NSString*) newname

{

if [_passwordlength] ==0 )

{

ret = zipOpenNewFileInZip_zipFile,

constchar*) [newnameUTF8String],//UTF-8方式编码

&zipInfo,

NULL,0,

NULL,0,

NULL,//comment

Z_DEFLATED,

Z_DEFAULT_COMPRESSION );

}

}

将上述代码中划线部分,替换为下面部分即可。

 constchar*) [newnamecStringUsingEncoding:CFStringConvertEncodingToNSStringEncodingkCFStringEncodingGB_18030_2000)],

已经在mac和windows两端测试通过,都可以正常压缩带有中文的文件,并能正常解压。

三、解压带有中文或者日文的压缩文件问题

以下转载自:网友:xin88yue   http://www.cocoachina.com/bbs/simple/?t10195.html

原因是:ZipArchive类的解压函数 -BOOL) UnzipFileTo:NSString*) path overWrite:BOOL) overwrite
在遍历压缩文件包,获取包文件的名字处理上存在Bug.

1.  下面两行代码获取包中当前文件的文件名
    unzGetCurrentFileInfo_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
    filename[fileInfo.size_filename] = ”;  //未尾追加0 结束

        此时获取的文件名是正确的.

2.     但是由char*获取得到NSString*的转换方法使用出错,Mac默认是按UTF8编码的

    //NSString * strPath = [NSString  stringWithCString:filename];       //此处得到的 strPath为空,导致函数返回YES,但目录下无文件

NSStringEncoding enc = CFStringConvertEncodingToNSStringEncodingkCFStringEncodingGB_18030_2000);
        NSString * strPath = [NSString  stringWithCString:filename encoding:enc];//正确!这个地方应该是跟压缩时的编码对应上才对。

这样就OK了!

Published by

风君子

独自遨游何稽首 揭天掀地慰生平