您好,登錄后才能下訂單哦!
iOS 數據壓縮與解壓
Hi,推薦文件給你 "數據壓縮與解壓.zip"
http://vdisk.weibo.com/s/Gbabp
本文中需要的第三庫在本文的代碼例子中可以下載。minizip和ZipArchive這兩個第三庫
ViewController.h代碼如下:
#import <UIKit/UIKit.h> #import "ZipArchive.h" @interface ViewController : UIViewController -(IBAction)compress:(id)sender; -(IBAction)unAr:(id)sender; @end
ViewController.m代碼如下:
自己拖2個Button與如下兩個方法相關聯
//這方法中數據壓縮的方法:
-(IBAction)compress:(id)sender { //導入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"]; //創建壓縮包 BOOL ret = [zip CreateZipFile2:zipfilePath]; //向壓縮包內加入數據 ret = [zip addFileToZip:p_w_picpath2 newname:@"xiaonan.jpg"]; ret = [zip addFileToZip:p_w_picpath3 newname:@"p_w_picpath3.jpg"]; //關閉壓縮包 [zip CloseZipFile2]; [zip release]; }
//數據解壓的方法
-(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]; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。