Search This Blog

Monday, 7 April 2014

Unzip File -iOS

Hello,

So , after a long research i found nothing which can unzip the zip file with subfolders and other files.So i am providing you a function which will do the job. Also, i assume you can setup the Objective-zip Library very well,for those who don't have the idea here is the link for tutorial and here is the link to download.


Below is the code , which is self explanatory, you just need your zip file Document directory root.


-(void)UnzipFile{
   
    NSArray *dirPaths;
    NSString *docsDir;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:[NSString stringWithFormat:@"Facility Manual.zip"]];
   
    ZipFile *zipF = [[ZipFile alloc]initWithFileName:soundFilePath mode:ZipFileModeUnzip];
    //Listing all files in zip file
   
    NSArray *infos = [zipF listFileInZipInfos];
    [loadingView setHidden:NO];
    bytesReceived.text  = @"Unzipping Files";
    [progress setHidden:YES];
    for(FileInZipInfo *fileInfo in infos){
       
        //Identifying if file in Zip is a folder or pdf
        NSRange range = [fileInfo.name rangeOfString:@".pdf" options:NSCaseInsensitiveSearch];
        if(range.length>0){
           
        //File found in zip locating the file in zip file
        [zipF locateFileInZip:fileInfo.name];
       
        //Read Stream reading current File in zip file
        ZipReadStream *readStream = [zipF readCurrentFileInZip];
       
        //Initializing and declaring buffer to read the data from zip file for current file
        NSMutableData *data = [[NSMutableData alloc]initWithLength:256];
       
        //Parsing the Name provided by zip file to get the file name
        NSArray *bits = [fileInfo.name componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
           
        //Building file path on local Drive where it will be unzipped.
        NSString *documentdir;
        NSArray *documentPaths;
         
        documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentdir = [documentPaths objectAtIndex:0];
           
        //Final path of file in local drive
        NSString *yoyoDir = [documentdir stringByAppendingPathComponent:[NSString stringWithFormat:@"All Files/%@",[bits objectAtIndex:bits.count-1] ]];
           
        //File Manager creating the file in local drive
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager createFileAtPath:yoyoDir contents:nil attributes:nil];
           
        //Writing data in file created on Local Drive
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:yoyoDir];
           
            do{
                //Reset Buffer
                [data setLength:256];
               
                int bytesRead = [readStream readDataWithBuffer:data];
                if(bytesRead>0){
                    [data setLength:bytesRead];
                        [fileHandle writeData:data];
                }else{
                    break;
                }
               

            }while(YES);
        //Releasing buffer and FileManager
        [fileHandle closeFile];
        fileManager = nil;
        data = nil;
           
        //Finishing the Read Stream reading.
        [readStream finishedReading];
        }
    }

    //Close the zipFile prevents from corrupt zip.
    [zipF close];
   
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    [fileMgr removeItemAtPath:soundFilePath error:NULL];
    [loadingView setHidden:YES];
   
}


Happy Programming ... ;)

No comments:

Post a Comment