如何使用“ZipArchive库”压缩文件在iphone? [英] How to zip file using "ZipArchive library" in iphone ?

查看:173
本文介绍了如何使用“ZipArchive库”压缩文件在iphone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在我的项目中实施 ZipArchive

I want to implement ZipArchive in my project.

使用这个库我想将图像文件存档为zip文件。

Using this library I want to archive files like images into zip file.

我该如何做?

推荐答案

试用我的代码
Zipcode:

try my code Zipcode :

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* dPath = [paths objectAtIndex:0];
    NSString* txtfile = [dPath stringByAppendingPathComponent:@"test.txt"];
    NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"];
    ZipArchive* zip = [[ZipArchive alloc] init];
    BOOL ret = [zip CreateZipFile2:zipfile];
    ret = [zip addFileToZip:txtfile newname:@"test.txt"];//zip
    if( ![zip CloseZipFile2] )
    {
        zipfile = @"";
    }
    [zip release];
    NSLog(@"The file has been zipped");

解压缩:

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* dPath = [paths objectAtIndex:0];

    NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"] ;
    NSString* unzipto = [dPath stringByAppendingPathComponent:@"test"] ;
    ZipArchive* zip = [[ZipArchive alloc] init];
    if([zip UnzipOpenFile:zipfile] )
    {
        BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
        if(NO == ret)
        {
        }
        [zip UnzipCloseFile];
    }
    [zip release];
    NSLog(@"The file has been unzipped");

这篇关于如何使用“ZipArchive库”压缩文件在iphone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆