ZipArchive和SSZipArchive使用詳解

閱讀目錄html

回到頂部git

1、SSZipArchive

 

1.簡介

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

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

 

 

2.壓縮方法

壓縮指定文件代碼:spa

複製代碼

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.解壓方法

代碼:htm

複製代碼

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 }

複製代碼

 

回到頂部對象

2、ZipArchive

 

1.簡介

ZipArchive能夠解壓和壓縮blog

 

2.壓縮方法

代碼:ip

複製代碼

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 }
相關文章
相關標籤/搜索