2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrew Ho
  • 618
  • 9
  • 21
  • You are unzipping the file to a folder and saving the folder path to NSUserDefaults, but then you retrieve this folder path again to be opened with ReaderDocument. Isn't the ReaderDocument asking for pdf file path instead of folder path? – Valent Richie May 28 '13 at 12:57
  • please check this:-http://stackoverflow.com/questions/11333399/download-and-unzip-file-in-ios – Nitin Gohel May 28 '13 at 12:59
  • yes ReaderDocument is asking for pdf file path instead of folder path. When i add the filename to the path it wont open either. I added the code: NSString *pdfbestand = [destinationPath stringByAppendingString:@"catalogus.pdf"]; [[NSUserDefaults standardUserDefaults] setValue:pdfbestand forKey:@"catalogus"]; Still not working – Andrew Ho May 28 '13 at 13:10
  • 1
    You might need to log and check if pdfbestand contains the correct path to the pdf file. And also check the documentsDir whether the pdf file is there. – Valent Richie May 28 '13 at 13:28

1 Answers1

4

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;
}
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61