How to unzip a .zip file on iOS? [duplicate]

牧云@^-^@ 提交于 2019-11-27 12:32:55

问题


After StoreKit downloads the IAP content package it returns an NSURL to me which looks like this:

file://localhost/private/var/mobile/Applications/45EF2B3A-3CAB-5A44-4B4A-631A122A4299/Library/Caches/BA32BC55-55DD-3AA4-B4AC-C2A456622229.zip/

Despite all sources I found claiming that StoreKit unzips the content package once downloaded, it hands me over a ZIP. This ZIP probably contains the file structure of the content package. But how do I unzip this?


回答1:


Use SSZipArchive

You can unzip using this

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:@"/ImagesFolder"];

NSString *zipPath = Your zip file path;

[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];

Hope it helps you.




回答2:


There is a great 3rd party tool for zipping/unzipping files for iPhone

https://github.com/soffes/ssziparchive

Very simple to use. Hope that helps!!

Edit:

Quick method I created which takes url, downloads the zip and unzips it

-(void)downloadAndUnzip : (NSString *)sURL_p : (NSString *)sFolderName_p
{
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(q, ^{
        //Path info
        NSURL *url = [NSURL URLWithString:sURL_p];
        NSData *data = [NSData  dataWithContentsOfURL:url];
        NSString *fileName = [[url path] lastPathComponent];
        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        [data writeToFile:filePath atomically:YES];
        dispatch_async(main, ^


                 {
                       //Write To
                       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                       NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
                       NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:sFolderName_p];

                       [SSZipArchive unzipFileAtPath:filePath toDestination:dataPath];

                   });
});

}


来源:https://stackoverflow.com/questions/16105072/how-to-unzip-a-zip-file-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!