iOS - Unzip and Unrar in Objective-C

Stephen Zaharuk / Friday, December 06, 2013

In a few of my own apps, i've needed the ability to unzip and unrar some files. Finding a good reliable library can be tough, so i'm going to recommend the ones that i've found and actually still use today. 

Unrar4iOS

Note: this library just unarchives rar files, it will not actually create a rar file:

Using this library is pretty simple, here's a snippet for unraring a file:

Unrar4iOS* unrar = [[Unrar4iOS alloc] init];

if ([unrar unrarOpenFile:filePath])
{
    NSArray *fileNames = [unrar unrarListFiles];
    if(fileNames != nil)
    {
        for (NSString *filename in fileNames)
        {
            NSData *data = [unrar extractStream:filename];

            if (data != nil)
            {
                 // Do Something With the File
            }

        }

    }
}

[unrar unrarCloseFile];

Objective-Zip

This library can be used to both unzip and zip files

Here is a snippet for using this library to unzip a file.

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
[unzipFile goToFirstFileInZip];

for (FileInZipInfo *info in infos)
{
    NSString *filename= info.name;
    ZipReadStream *read1= [unzipFile readCurrentFileInZip];

    NSMutableData *data= [[NSMutableData alloc] initWithLength:info.length];
    NSUInteger bytesRead = [read1 readDataWithBuffer:data];

    if(bytesRead > 0)
    {
        // Do something with the file.
    }


    [read1 finishedReading];
    [unzipFile goToNextFileInZip];
}

[unzipFile close];
[unzipFile release];

As always, I hope you found this useful. 

By Stephen Zaharuk (SteveZ)