Apple开发_SSZipArchive库的使用

1、简介

  • SSZipArchive是iOS和Mac上一个简单实用的压缩和解压插件。用途包括:
    • 1.解压zip文件;
    • 2.解压密码保护的ZIP文件;
    • 3.创建新的zip文件;
    • 4.追加文件到现有的压缩;
    • 5.压缩文件;
    • 6.压缩NSData(带有文件名)
  • SSZipArchive的GitHub地址:https://github.com/ZipArchive/ZipArchive

2、压缩方法

  • 压缩指定文件代码:
复制// SSZipArchive压缩
- (void)ssZipArchiveWithFiles {
    // Caches路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
   // zip压缩包保存路径
    NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
    // 需要压缩的文件
    NSArray *filesPath = @[
                          @"/Users/apple/Desktop/demo/LaunchImage-2-700-568h@2x.png",
                          @"/Users/apple/Desktop/demo/LaunchImage-2-700@2x.png",
                          @"/Users/apple/Desktop/demo/LaunchImage-2-800-667h@2x.png",
                          @"/Users/apple/Desktop/demo/LaunchImage-2-800-Landscape-736h@3x.png"
                          ];
    // 创建不带密码zip压缩包
    BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath];
    // 创建带密码zip压缩包
    //BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath withPassword:@"SSZipArchive.zip"];
}
  • 压缩指定文件夹代码:
// SSZipArchive压缩
- (void)ssZipArchiveWithFolder {
	// Caches路径
	NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
	// zip压缩包保存路径
	NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
	// 需要压缩的文件夹路径
	NSString *folderPath = @"/Users/apple/Desktop/demo/";
	// 创建不带密码zip压缩包
	BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath ];
	// 创建带密码zip压缩包
	//BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath withPassword:@"SSZipArchive.zip"];
}

3、解压方法

  • 代码:
// SSZipArchive解压
-(void)uSSZipArchive {
    // Caches路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    // 解压目标路径
    NSString *destinationPath = [cachesPath stringByAppendingPathComponent:@"SSZipArchive"];
    // zip压缩包的路径
    NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
    // 解压
    BOOL isSuccess = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath];

	// 带密码的解压
    // 出错原因
    NSError *error;
    // 解压
    BOOL unzip_ok = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath
                                        overwrite:YES password:@"123456" error:&error];
}

4、可能遇到的问题

posted @   CH520  阅读(472)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示
哥伦布
08:13发布
哥伦布
08:13发布
2°
西北风
4级
空气质量
相对湿度
89%
今天
小雪
0°/6°
周日
2°/12°
周一
4°/15°