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 }
学习,以记之。如有错漏,欢迎指正

作者:冯子武
出处:http://www.cnblogs.com/Zev_Fung/
本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
如果博文对您有所收获,请点击下方的 [推荐],谢谢

原文地址:https://www.cnblogs.com/Zev_Fung/p/5595265.html