IOS 使用ZipArchive解壓文件

引入框架 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];

//記得釋放

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容