iOS 開發_壓縮·解壓縮ZipArchive進度詳解

【作者前言】:分享些本人工作中遇到的點點滴滴那些事兒,剛開始寫博客,高手勿噴!以分享交流為主,歡迎各路豪杰點評改進!

1.應用場景:

很多時候,需要我們對App端包的大小進行瘦身,這個時候最直觀方案就是將文件比較大的東東扔到服務端,經App下載后,在做二次擴容以支撐App的完整功能體驗!

2.實現目標:

使用ZipArchive實現壓縮,解壓縮。 并對進度進行監聽!

3.代碼說明:

!!! 引入到工程時需要添加 libz.tbd 庫,否則編譯時通不過。 · !!!使用時,比較大的文件可以考慮新開辟子線程處理,避免耗時影響主線程流暢

//壓縮
- (void)createZipFile {  
    //壓縮完成后,zip文件存放的路徑  
    NSString *destinationPath = @"***/zyp.zip";
    //需要壓縮的文件路徑  
    NSString *sourceFilePath = @***/zyp.txt";  
    //數組里可以放多個源文件,這些文件會被同一打包成壓縮包,到 destinationPath 這個路徑下。  
    if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {  
        NSLog(@"壓縮成功");  
    }  
    else {  
        NSLog(@"壓縮失敗");  
    }  
}  
//解壓縮
- (void)unzipFile {  
    //需要解壓的zip文件路徑  
    NSString *sourceFilePath = @"***/zyp.zip";  
    //解壓完成后,存放的文件路徑  
    NSString *destinationPath = @"***/zypUnAr/";  
    //把 sourceFilePath 這個路徑下的zip包,解壓到這個 destinationPath 路徑下  
    if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]){  
        NSLog(@"解壓成功");  
    }  
    else {  
        NSLog(@"解壓失敗");  
    }  
}  
!!!可能遇到的問題
//當對要壓縮或者要解壓的文件的文件名包含有中文文字時,
這個時候會出現文件名亂碼的問題,或者在目的路徑下未能找到解壓后的文件的問題。
## 解決辦法:

在 SSZipArchive.m 文件中改一下對 文件路徑的編碼格式,即可。

更改前:
NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];
更改后:
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString *strPath = [NSString stringWithCString:filename encoding:enc];

常用代理方法
#pragma mark - ?? SSZipArchiveDelegate  
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {  
    NSLog(@"將要解壓。");  
}  
  
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {  
    NSLog(@"解壓完成!");  
}  

全部代理方法標注:

@interface SSZipArchive : NSObject  
  
// Unzip 解壓  
/** 
 * @param          path    源文件 
 * @param   destination    目的文件 
 * @param      uniqueId    標記,用于區別多個解壓操作 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示解壓失敗。 
 */  
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination  uniqueId:(NSString *)uniqueId;  
  
/** 
 * @param          path    源文件 
 * @param   destination    目的文件 
 * @param     overwrite    YES 會覆蓋 destination 路徑下的同名文件,NO 則不會。 
 * @param      password    需要輸入密碼的才能解壓的壓縮包 
 * @param         error    返回解壓時遇到的錯誤信息 
 * @param      uniqueId    標記,用于區別多個解壓操作 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示解壓失敗。 
 */  
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error  uniqueId:(NSString *)uniqueId;  
  
/** 
 * @param          path    源文件 
 * @param   destination    目的文件 
 * @param      delegate    設置代理 
 * @param      uniqueId    標記,用于區別多個解壓操作 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示解壓失敗。 
 */  
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate  uniqueId:(NSString *)uniqueId;  
  
/** 
 * @param          path    源文件 
 * @param   destination    目的文件 
 * @param     overwrite    YES 會覆蓋 destination 路徑下的同名文件,NO 則不會。 
 * @param      password    需要輸入密碼的才能解壓的壓縮包 
 * @param         error    返回解壓時遇到的錯誤信息 
 * @param      delegate    設置代理 
 * @param      uniqueId    標記,用于區別多個解壓操作 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示解壓失敗。 
 */  
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;  
  
// Zip 壓縮  
/** 
 * @param       path    目的路徑(格式:~/xxx.zip 結尾的路徑) 
 * @param  filenames    要壓縮的文件路徑 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示壓縮失敗。 
 */  
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;  
  
/** 
 * @param       path    目的路徑(格式:~/xxx.zip 結尾的路徑) 
 * @param  filenames    要壓縮的文件目錄路徑 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示壓縮失敗。 
 */  
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;  
  
/** 
 * 初始化壓縮對象 
 * 
 * @param  path    目的路徑(格式:~/xxx.zip 結尾的路徑) 
 * 
 * @return 初始化后的對像 
 */  
- (id)initWithPath:(NSString *)path;  
  
/** 
 *  打開壓縮對象 
 * @return 返回 YES 表示成功,返回 NO 表示失敗。 
 */  
- (BOOL)open;  
  
/** 
 * 添加要壓縮的文件的路徑 
 * 
 * @param  path    文件路徑 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示失敗。 
 */  
- (BOOL)writeFile:(NSString *)path;  
  
/** 
 * 向此路徑的文件里寫入數據 
 * 
 * @param      data    要寫入的數據 
 * @param  filename    文件路徑 
 * 
 * @return 返回 YES 表示成功,返回 NO 表示失敗。 
 */  
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename;  
  
/** 
 *  關閉壓縮對象 
 * @return 返回 YES 表示成功,返回 NO 表示失敗。 
 */  
- (BOOL)close;  
  
@end  
  
  
@protocol SSZipArchiveDelegate <NSObject>  
  
@optional  
  
//將要解壓  
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;  
//解壓完成  
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId;  
//將要解壓  
- (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;  
//解壓完成  
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;  
  
@end  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容