关于文件压缩与解压缩的那些事zipArchive

/**

     *  压缩文件的步骤,首先需要把zipArchive导入工程中,并添加 libz.dylib

     *

     * 创建压缩文件:

     * 1.制定一个路径

     * 2.利用方法 CreateZipFile2:创建压缩文件

     * 3.利用方法 CreateZipFile2: Password:创建带有密码的压缩文件

     * 4.给压缩文件中添加 文件

     * 4.关闭 CloseZipFile2(只有调用了这个方法,zip文件才会从内存中写入磁盘)

     *

     * 解压文件:

     * 1. 解压文件:UnzipOpenFile

     * 2. 把解压的文件存放到一个制定的目录:UnzipFileTo: overWrite

     * 3. 解压带有密码的文件

     *

     * 难点:

     * 文件路径错误,会导致意想不到的错误

     *

     * 代码说明:

     * 下面的代码主要的应用场景是,从网络上请求数据写入到压缩文件中,对这个压缩文件解压,得到一个密码和另外一个压缩文件,拿到密码后

     * 对得到的压缩文件进行解压。

     */

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [paths firstObject];

    

    

    

    NSString *filepath = [documentPath stringByAppendingPathComponent:@"wsy.zip"];

    NSString *encrypt = [documentPath stringByAppendingPathComponent:@"encrypt"];

    

    NSString *filepath2 = [documentPath stringByAppendingPathComponent:@"wsy/wsy.zip"];

    NSString *content = [documentPath stringByAppendingPathComponent:@"wsy/content"];

    NSString *image = [documentPath stringByAppendingPathComponent:@"wsy/image.jpg"];

    

    // documents下创建一个文件

    // ~~~~~~/documents/firmail

    NSString *firmailPath = [documentPath stringByAppendingPathComponent:@"wsy"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    [fileManager createDirectoryAtPath:firmailPath withIntermediateDirectories:YES attributes:nil error:nil];

    

    ZipArchive *zip2 = [[ZipArchive alloc] init];

    // 创建压缩文件,带有密码

    BOOL ret3 = [zip2 CreateZipFile2:filepath2 Password:@"wsy"];

    // 路径:documents/firmail/firmail.zip

    // contentattachments/img.jpg加入到压缩文件中

    // firmail.zip中包含了contentttachments/img.jpg这两个文件

    

    // 给压缩文件中添加内容

    ret3 = [zip2 addFileToZip:content newname:@"content"];

    ret3 = [zip2 addFileToZip:image newname:@"attachments/img.jpg"];

    if (![zip2 CloseZipFile2]) {

        filepath2 = @"";

    }

    

    ZipArchive *archive = [[ZipArchive alloc] init];

    // 创建不带密码的压缩文件

    ret3 = [archive CreateZipFile2:filepath];

    // 路径:documents/firmail.zip

    // encrypt firmail.zip 加入到压缩文件中

    // firmail.zip中包含了contentfirmail.zip这两个文件

    

    ret3 = [archive addFileToZip:encrypt newname:@"encrypt"];

    ret3 = [archive addFileToZip:filepath2 newname:@"wsy.zip"];

    ret3 = [archive addFileToZip:[documentPath stringByAppendingPathComponent:@"image"] newname:@"header.jpg"];

    if (![archive CloseZipFile2]) {

        filepath2 = @"";

    }

    

    // 解压文件

    ZipArchive *unZip = [[ZipArchive alloc] init];

    // 把解压文件放入到这个路径下

     NSString *unZipPath = [documentPath stringByAppendingPathComponent:@"wsy"];

    ret3 = [unZip UnzipOpenFile:filepath];

    if (ret3) {

        // 把解压文件放入到一个路径下

        ret3 = [unZip UnzipFileTo:unZipPath overWrite:YES];

        if (ret3) {

            NSLog(@"解压成功");

            NSString *unzipPath2 = [unZipPath stringByAppendingPathComponent:@"wsy"];

            NSString *secretFirmailZip = [unZipPath stringByAppendingPathComponent:@"wsy.zip"];

            ZipArchive *unZipSecretFrimail = [[ZipArchive alloc] init];

            if ([unZipSecretFrimail UnzipOpenFile:secretFirmailZip]) {

                ret3 = [unZipSecretFrimail UnzipFileTo:unzipPath2 overWrite:YES];

                if (ret3) {

                    NSLog(@"二次解压成功");

                }

            }

        }else{

            NSLog(@"解压失败");

        }

        // 关闭解压

        [unZip UnzipCloseFile];

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值