iPhone: Downloading zip and extracting in main bundle subdirectory at runtime

后端 未结 4 1811
夕颜
夕颜 2020-12-12 21:19

I want to extend my iPhone app that the app downloads a zip file into a sub directory then extracts it and then load images which were inside the zip.

Any ideas how

相关标签:
4条回答
  • 2020-12-12 21:46

    I've used ZipArchive with success in the past.

    It's pretty ligthweight and simple to use, supports password protection, multiple files inside a ZIP, as well as compress & decompress.

    The basic usage is:

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
    ZipArchive *zipArchive = [[ZipArchive alloc] init];
    [zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"];
    [zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES];
    [zipArchive UnzipCloseFile];
    [zipArchive release];
    
    0 讨论(0)
  • 2020-12-12 22:06

    I suggest you to use ssziparchive bcoz it support both ARC and Non ARC project.

    1. Add SSZipArchive.h, SSZipArchive.m, and minizip to your project.
    2. Add latest version of the libz(right now its libz1.2.5) library to your target.

    You don't need to do anything regarding ARC. SSZipArchive will detect if you're not using ARC and add the required memory management code.

    And code will be like this:

        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
        // Unzipping
    
        //If your zip is in document directory than use this code
        NSString *zipPath = [documentsDirectory stringByAppendingPathComponent:@"mediadata.zip"];
        //else if zip file is in bundle than use this code
        NSString *zipPath = [[NSBundle mainBundle] pathForResource:@"mediadata" ofType:@"zip"];
    
        NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"MediaData"];
    
    
        if( [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath] != NO ) {
            //unzip data success
            //do something
            NSLog(@"Dilip Success");
        }else{
            NSLog(@"Dilip Error");
        }
    
    0 讨论(0)
  • 2020-12-12 22:07

    If you add ZipArchive to your project, remember to also add the framework. The correct framework is libz.dylib. Latest version (Xcode 4.2) is 1.2.5.

    (Add framework to the section with libraries in the build phases tab of your target.)

    0 讨论(0)
  • 2020-12-12 22:12

    You cannot extract into your bundle. Use [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] to get the path to a directory you can write to.

    You can use the code from http://code.google.com/p/ziparchive/ to extract files from the zip archive.

    0 讨论(0)
提交回复
热议问题