iOS 數(shù)據(jù)壓縮與解壓
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供東海網(wǎng)站建設(shè)、東海做網(wǎng)站、東海網(wǎng)站設(shè)計(jì)、東海網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、東海企業(yè)網(wǎng)站模板建站服務(wù),10余年東海做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Hi,推薦文件給你 "數(shù)據(jù)壓縮與解壓.zip"
http://vdisk.weibo.com/s/Gbabp
本文中需要的第三庫在本文的代碼例子中可以下載。minizip和ZipArchive這兩個(gè)第三庫
ViewController.h代碼如下:
#import#import "ZipArchive.h" @interface ViewController : UIViewController -(IBAction)compress:(id)sender; -(IBAction)unAr:(id)sender; @end
ViewController.m代碼如下:
自己拖2個(gè)Button與如下兩個(gè)方法相關(guān)聯(lián)
//這方法中數(shù)據(jù)壓縮的方法:
-(IBAction)compress:(id)sender { //導(dǎo)入ZipArchive包之后要加入libz.1.2.5.dylib的庫 ZipArchive* zip = [[ZipArchive alloc] init]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; NSString* zipfilePath = [documentpath stringByAppendingString:@"/dj.zip"] ; // NSLog(@"%@",zipfilePath); NSString* p_w_picpath2 = [[NSBundle mainBundle] pathForResource:@"xiaonan" ofType:@"jpg"]; NSString* p_w_picpath3 = [[NSBundle mainBundle] pathForResource:@"xiaonan" ofType:@"jpg"]; //創(chuàng)建壓縮包 BOOL ret = [zip CreateZipFile2:zipfilePath]; //向壓縮包內(nèi)加入數(shù)據(jù) ret = [zip addFileToZip:p_w_picpath2 newname:@"xiaonan.jpg"]; ret = [zip addFileToZip:p_w_picpath3 newname:@"p_w_picpath3.jpg"]; //關(guān)閉壓縮包 [zip CloseZipFile2]; [zip release]; }
//數(shù)據(jù)解壓的方法
-(IBAction)unAr:(id)sender { 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:@"/dj.zip"] ; NSString* unzipto = [documentpath stringByAppendingString:@"/dj"] ; //找到需要解壓文件的路徑 if( [zip UnzipOpenFile:l_zipfile] ) { BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES]; if( NO==ret ) { } //完成解壓 [zip UnzipCloseFile]; } [zip release]; }