zoukankan      html  css  js  c++  java
  • ZipArchive(解压文件)

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

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

    [cpp] view plaincopy
     
    1. ZipArchive* zip = [[ZipArchive alloc] init];  
    2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    3. NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  
    4. NSString* l_zipfile = [documentpath stringByAppendingString:@"/test.zip"] ;  
    5.    
    6. NSString* image1 = [documentpath stringByAppendingString:@"/image1.jpg"] ;  
    7. NSString* image2 = [documentpath stringByAppendingString:@"/image2.jpg"] ;         
    8.    
    9. BOOL ret = [zip CreateZipFile2:l_zipfile];  
    10. ret = [zip addFileToZip:image1 newname:@"image1.jpg"];  
    11. ret = [zip addFileToZip:image2 newname:@"image2.jpg"];  
    12. if( ![zip CloseZipFile2] )  
    13.   {  
    14.      l_zipfile = @"";  
    15.   }  
    16. [zip release];  



     

    2. 解压缩:

    [cpp] view plaincopy
     
    1. ZipArchive* zip = [[ZipArchive alloc] init];  
    2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    3. NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  
    4.    
    5. NSString* l_zipfile = [documentpath stringByAppendingString:@"/test.zip"] ;  
    6. NSString* unzipto = [documentpath stringByAppendingString:@"/test"] ;  
    7. if( [zip UnzipOpenFile:l_zipfile] )  
    8.  {  
    9.    BOOLret = [zip UnzipFileTo:unzipto overWrite:YES];  
    10.    if( NO==ret )  
    11.    {  
    12.    }  
    13.    [zip UnzipCloseFile];  
    14.  }  
    15. [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:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_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 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
            NSString * strPath = [NSString  stringWithCString:filename encoding:enc];//正确!这个地方应该是跟压缩时的编码对应上才对。

    这样就OK了!

  • 相关阅读:
    字典的操作用法小总结
    HTTP Headers解析
    RStdio常用快捷键
    R语言数据类型
    数据科学实战手册(R+Python)书中引用资料网址
    ggplot2使用初探
    urllib2使用初探
    R语言以及RStdio的安装
    目标检测--Selective Search for Object Recognition(IJCV, 2013)
    关于Python的lambda
  • 原文地址:https://www.cnblogs.com/mgbert/p/3993248.html
Copyright © 2011-2022 走看看