Search code examples
iosxcodezipunzipssziparchive

SSZiparchive simple unarchiving


I am building an app which downloads a bunch of data from a zip file. I downloaded the zipfile but want to extract the catalog which is a pdf file. I am using SSZipArchive but my code isn't working. I guess I'm not unzipping it correctly.

My code:

[data writeToFile:filePath atomically:YES];

// Unzipping
NSString *destinationPath = [documentsDir stringByAppendingPathComponent:@"catalogus"];;
[SSZipArchive unzipFileAtPath:filePath toDestination:destinationPath];
[[NSUserDefaults standardUserDefaults] setValue:destinationPath forKey:@"catalogus"];

And in another viewcontroller I want to open the pdf

NSString *bestand = [[NSUserDefaults standardUserDefaults] valueForKey:@"catalogus"];
ReaderDocument *testdocument = [ReaderDocument withDocumentFilePath:bestand password:phrase];

When i download the pdf directly (not zipped at all) my code works fine.


Solution

  • Use this to unzip :

    - (void)Unzipping {
    
        NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:@"/PDFFolder"];
    
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"FILENAME.zip"];
        NSString *zipPath = filePath;
    
        [SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];
    
    }
    

    and use this method to fetch pdf from directory :

    -(NSData *)FetchPDF
    {
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/PDFFolder"];
        NSString *workSpacePath = [dataPath stringByAppendingPathComponent:@"your_filename.pdf"];
        NSData *pdfData = [NSData dataWithContentsOfFile:workSpacePath];
    
        return pdfData;
    }