一、使用詳解:
我們在開發app的時候,有時會需要對文件進行壓縮和解壓的操作,這個時候我們就必須要用到一個第三方的開源庫,SSZipArchive ,來對目標文件進行壓縮和解壓的操作。
使用了DDLog記錄日誌 , DDLog的日誌按天存在不同的文件中(設置的保存7天的日誌) , 以前的上傳會用NSData循環讀取這7個文件生成一個大的NSData上傳解壓之後成爲了一個文件. 但是我們找某一天的日誌時很費力 , 所以想要解壓之後還是原來的7個文件,所以需要把log文件夾下的文件壓成一個文件,然後上傳這一個文件,解壓之後還是文件夾,然後在找某天的日誌就很清楚了.nice.
github下載地址 , 推薦使用pod集成,一開始我手動集成的,但是編譯通過,壓縮的文件總是損壞,後來換成pod集成就好了
//壓縮
- (void)createZipFile {
//目的路徑
NSString *destinationPath = @"/Users/Administrator/Desktop/wzg.zip";//注意是這個是 zip 格式的後綴
//源文件路徑
NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.txt";
//數組裏可以放多個源文件,這些文件會被同一打包成壓縮包,到 destinationPath 這個路徑下。
if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {
NSLog(@"壓縮成功");
}
else {
NSLog(@"壓縮失敗");
}
}
//解壓
- (void)unzipFile {
//源文件路徑
NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.zip";
//目的文件路徑
NSString *destinationPath = @"/Users/wangzhengang/Desktop/";
//把 sourceFilePath 這個路徑下的zip包,解壓到這個 destinationPath 路徑下
if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]){
NSLog(@"解壓成功");
}
else {
NSLog(@"解壓失敗");
}
}
代理方法:
#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(@"解壓完成!");
}
二、SSZipArchive所有方法說明:
@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
三、遇到的問題:
當對要壓縮或者要解壓的文件的文件名包含有中文文字時,這個時候會出現文件名亂碼的問題,或者在目的路徑下未能找到解壓後的文件的問題。
解決辦法:
在 SSZipArchive.m 文件中改一下對 文件路徑的編碼格式,即可。
更改前:
更改後: