压缩和解压缩(I)

时间:2023-03-09 01:44:27
压缩和解压缩(I)

ZipArchive

压缩方法

 -(void)zipArchiveWithFiles
{
//创建解压缩对象
ZipArchive *zip = [[ZipArchive alloc]init];
//Caches路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//zip压缩包保存路径
NSString *path = [cachesPath stringByAppendingPathComponent:@"ZipArchive.zip"];//创建不带密码zip压缩包
//创建zip压缩包
[zip CreateZipFile2:path];
//创建带密码zip压缩包
//[zip CreateZipFile2:path Password:@"ZipArchive.zip"];
   //添加到zip压缩包的文件
[zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-700-568h@2x.png" newname:@"1.png"];
[zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-700@2x.png" newname:@"2.png"];
[zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-800-667h@2x.png" newname:@"3.png"];
[zip addFileToZip:@"/Users/apple/Desktop/demo/LaunchImage-2-800-Landscape-736h@3x.png" newname:@"4.png"];
//关闭压缩
BOOL success = [zip CloseZipFile2];
}

解压缩方法

 -(void)uZipArchive
{
//创建解压缩对象
ZipArchive *zip = [[ZipArchive alloc]init];
//Caches路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
  //解压目标路径
NSString *savePath =[cachesPath stringByAppendingPathComponent:@"ZipArchive"];
  //zip压缩包的路径
NSString *path = [cachesPath stringByAppendingPathComponent:@"ZipArchive.zip"];
//解压不带密码压缩包
[zip UnzipOpenFile:path];
//解压带密码压缩包
//[zip UnzipOpenFile:path Password:@"ZipArchive.zip"];
//解压
[zip UnzipFileTo:savePath overWrite:YES];
//关闭解压
BOOL success = [zip UnzipCloseFile];
}