Sunday, October 21, 2012

Implement ZIP-Archive in IOS

Implement ZipArchive in iOS project.


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

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

No comments:

Post a Comment