引入框架 libz.dylib
引入頭文件 "ZipArchive.h"
- (IBAction) unZipClick {
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"book_%d.zip", bookID]];
NSString *unZipPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"book_%d", bookID]];
ZipArchive *zip = [[ZipArchive alloc] init];
BOOL result;
if ([zip UnzipOpenFile:filePath]) {
result = [zip UnzipFileTo:unZipPath overWrite:YES];
if (!result) {
NSLog(@"解壓失敗");
}
else
{
readBtn.enabled = YES;
NSLog(@"解壓成功");
}
[zip UnzipCloseFile];
}
}
創(chuàng)建/添加一個(gè)zip包
ZipArchive* zipFile = [[ZipArchive alloc] init];
//次數(shù)得zipfilename需要一個(gè)完整得路徑,例如***/Documents/demo.zip
[zipFile CreateZipFile2:@"zipfilename"];
//有兩種可選得方式進(jìn)行創(chuàng)建壓縮包,帶密碼和不帶密碼的
[[zipFile CreateZipFile2:@"zipfilename" Password:@"your password"];
//接下來(lái)就是將需要壓縮的文件添加到這個(gè)壓縮包中
//這里第一個(gè)參數(shù)需要完整的路徑,例如:***/Documents/a.txtnewname是指文件在壓縮包中的名字,不需要路徑,只是一個(gè)名稱
[zipFile addFileToZip:@"fullpath of the file" newname:@"new name of the file without path"];
//如果需要將多個(gè)文件進(jìn)行壓縮,即壓縮文件夾,重復(fù)addFileToZip方法即可
[zipFile CloseZipFile2];
[zipFile release];
//釋放內(nèi)存
解壓zip包:
ZipArchive* zipFile = [[ZipArchive alloc] init];
[zipFile UnzipOpenFile:@"zip file name"];
//同樣,對(duì)應(yīng)的就有兩種打開(kāi)zip包的方式,帶密碼和不帶密碼
[zipFile UnzipOpenFile:@"zip file name" Password:@"password" ];
//壓縮包釋放到的位置,需要一個(gè)完整路徑
[zipFile UnzipFileTo:@"output path" overwrite:YES];
[zipFile UnzipCloseFile];
[zipFile release];
//記得釋放