Unzip folder and get Images from unzip file

202 Views Asked by At

I have successfully downloaded zip file and unzip it with the help of ZipArchive. But now I have to access images from sub-folder of unzip file. The unzip folder is saved in document directory. Sub folder name always changed. I have used following code to unzip the zip file :

    NSURL *zipUrl = [NSURL URLWithString:responseString];
    NSData *data = [NSData dataWithContentsOfURL:zipUrl options:0 error:&error];
    if(!error)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSLog(@"paths..%@",paths);
        NSString *path = [paths objectAtIndex:0];
        NSString *zipPath = [path stringByAppendingPathComponent:@"zipfile.zip"];

        [data writeToFile:zipPath options:0 error:&error];

        if(!error)
        {
            ZipArchive *za = [[ZipArchive alloc] init];
            if ([za UnzipOpenFile: zipPath])
            {
                BOOL ret = [za UnzipFileTo: path overWrite: YES];
                if (NO == ret){} [za UnzipCloseFile];
            }
        }
        else
        {
            NSLog(@"Error saving file %@",error);
        }
    }
    else
    {
        NSLog(@"Error downloading zip file: %@", error);
    }

Please help me out. Thanks in advance.

2

There are 2 best solutions below

0
Gabriel On

Use NSFileManager methods to inspect contents of unzip file. Try with

 - enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

for a deep enumeration.

Or

 -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

for a shallow enumeration of contents.

Then you can filter files using extension to find images.

0
yoshiiiiiiii On

Use

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

to get the directory listing.

You will get your subfolder name from directoryContent array.