1. First, introduce how to use minizip
Ziparchive is an objective-C Class Based on the open-source "minizip" Zip compression and decompression. It is very simple to use.
Method: Download ziparchive.zip from the http://code.google.com/p/ziparchive/, decompress it, add the code to the project, and add the zlib library to the project.
Usage:
1. Compression: ziparchive can compress multiple files. You only need to addfiletozip the files one by one.
ZipArchive * zip = [[ZipArchive alloc] init];
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, 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. Unzip:
ZipArchive * zip = [[ZipArchive alloc] init];
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, 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];
The above is excerpted from: http://www.cnblogs.com/a7345678/archive/2012/06/27/2566125.html Night Elves-Gui Cai Ge
2. When compressing files containing Chinese, garbled characters appear after decompression under windows.
The reason for this problem is that in the Ios version of ZipArchive project, the encoding format is changed to UTF-8. Then the encoding format on Windows is mostly GBK.
Then open the source code of ZipArchive and change the encoding method.
Find the function:
-(BOOL) addFileToZip: (NSString *) file newname: (NSString *) newname
{
if ([_passwordlength] == 0)
{
ret = zipOpenNewFileInZip (_zipFile,
(constchar *) [newnameUTF8String], // UTF-8 encoding
& zipInfo,
NULL, 0,
NULL, 0,
NULL, // comment
Z_DEFLATED,
Z_DEFAULT_COMPRESSION);
}
}
Replace the underlined part of the above code with the following part.
(constchar *) [newnamecStringUsingEncoding: CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000)],
It has been tested on both ends of mac and windows, and can normally compress files with Chinese, and can decompress normally.
3. The problem of decompressing compressed files with Chinese or Japanese
The following is reproduced from: Netizen: xin88yue http://www.cocoachina.com/bbs/simple/?t10195.html
The reason is: the decompression function of the ZipArchive class-(BOOL) UnzipFileTo: (NSString *) path overWrite: (BOOL) overwrite
There is a bug in the process of traversing the compressed file package and obtaining the name of the package file.
1. The following two lines of code get the file name of the current file in the package
unzGetCurrentFileInfo (_unzFile, & fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
filename [fileInfo.size_filename] = '\ 0'; // Append 0 to the end without ending
The file name obtained at this time is correct.
2. However, the conversion method of NSString * obtained by char * is wrong. Mac defaults to UTF8 encoding
// NSString * strPath = [NSString stringWithCString: filename]; // The strPath obtained here is empty, causing the function to return YES, but no files in the directory
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString * strPath = [NSString stringWithCString: filename encoding: enc]; // Correct! This place should correspond to the encoding during compression.
This is OK!