且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用“ZipArchive库”压缩文件在iphone?

更新时间:2023-02-22 18:13:02

试用我的代码
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");