ZipArchive和SSZipArchive使用詳解


一、SSZipArchive

1.簡介

SSZipArchive是iOS和Mac上一個簡單實用的壓縮和解壓插件。用途包括:
1.解壓zip文件;
2.解壓密碼保護的ZIP文件;
3.創建新的zip文件;
4.追加文件到現有的壓縮;
5.壓縮文件;
6.壓縮NSData(帶有文件名)

SSZipArchive的GitHub地址:https://github.com/ZipArchive/ZipArchive

 

2.壓縮方法

壓縮指定文件代碼:

 1 /**
 2  *  SSZipArchive壓縮
 3  */
 4 -(void)ssZipArchiveWithFiles
 5 {
 6     //Caches路徑
 7     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
 8    //zip壓縮包保存路徑
 9     NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
10     //需要壓縮的文件
11     NSArray *filesPath = @[
12                           @"/Users/apple/Desktop/demo/LaunchImage-2-700-568h@2x.png",
13                           @"/Users/apple/Desktop/demo/LaunchImage-2-700@2x.png",
14                           @"/Users/apple/Desktop/demo/LaunchImage-2-800-667h@2x.png",
15                           @"/Users/apple/Desktop/demo/LaunchImage-2-800-Landscape-736h@3x.png"
16                           ];
17     //創建不帶密碼zip壓縮包
18     BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath];
19     //創建帶密碼zip壓縮包
20     //BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath withPassword:@"SSZipArchive.zip"];
21 }

 

壓縮指定文件夾代碼:

 1 /**
 2  *  SSZipArchive壓縮
 3  */
 4 -(void)ssZipArchiveWithFolder
 5 {
 6     //Caches路徑
 7     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
 8   //zip壓縮包保存路徑
 9     NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
10     //需要壓縮的文件夾路徑
11     NSString *folderPath = @"/Users/apple/Desktop/demo/";
12     //創建不帶密碼zip壓縮包
13     BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath ];
14     //創建帶密碼zip壓縮包
15     //BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath withPassword:@"SSZipArchive.zip"];
16 }

 

3.解壓方法

代碼:

 1 /**
 2  *  SSZipArchive解壓
 3  */
 4 -(void)uSSZipArchive
 5 {
 6     //Caches路徑
 7     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
 8     //解壓目標路徑
 9     NSString *destinationPath =[cachesPath stringByAppendingPathComponent:@"SSZipArchive"];
10     //zip壓縮包的路徑
11     NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
12     //解壓
13     BOOL isSuccess = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath];
14 }

 

二、ZipArchive

1.簡介

ZipArchive可以解壓和壓縮

2.壓縮方法

代碼:

 1 /**
 2  *  ZipArchive壓縮
 3  */
 4 -(void)zipArchiveWithFiles
 5 {
 6     //創建解壓縮對象
 7     ZipArchive *zip = [[ZipArchive alloc]init];
 8     //Caches路徑
 9     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
10     //zip壓縮包保存路徑
11     NSString *path = [cachesPath stringByAppendingPathComponent:@"ZipArchive.zip"];//創建不帶密碼zip壓縮包
12   //創建zip壓縮包 13 [zip CreateZipFile2:path]; 14 //創建帶密碼zip壓縮包 15 //[zip CreateZipFile2:path Password:@"ZipArchive.zip"]; 16    //添加到zip壓縮包的文件 17 [zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-700-568h@2x.png" newname:@"1.png"]; 18 [zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-700@2x.png" newname:@"2.png"]; 19 [zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-800-667h@2x.png" newname:@"3.png"]; 20 [zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-800-Landscape-736h@3x.png" newname:@"4.png"]; 21 //關閉壓縮 22 BOOL success = [zip CloseZipFile2]; 23 }

 

3.解壓方法

代碼:

 1 /**
 2  *  ZipArchive解壓
 3  */
 4 -(void)uZipArchive
 5 {
 6     //創建解壓縮對象
 7     ZipArchive *zip = [[ZipArchive alloc]init];
 8     //Caches路徑
 9     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
10   //解壓目標路徑
11     NSString *savePath =[cachesPath stringByAppendingPathComponent:@"ZipArchive"];
12   //zip壓縮包的路徑
13     NSString *path = [cachesPath stringByAppendingPathComponent:@"ZipArchive.zip"];
14     //解壓不帶密碼壓縮包
15     [zip UnzipOpenFile:path];
16     //解壓帶密碼壓縮包
17     //[zip UnzipOpenFile:path Password:@"ZipArchive.zip"];
18     //解壓
19     [zip UnzipFileTo:savePath overWrite:YES];
20     //關閉解壓
21     BOOL success = [zip UnzipCloseFile];
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM