SSZiparchive简单的unarchiving

时间:2013-05-28 12:53:12

标签: ios xcode zip unzip ssziparchive

我正在构建一个从zip文件下载大量数据的应用程序。我下载了zipfile但想要提取目录,这是一个pdf文件。我正在使用SSZipArchive,但我的代码无效。我想我没有正确解开它。

我的代码:

[data writeToFile:filePath atomically:YES];

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

在另一个viewcontroller中,我想打开pdf

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

当我直接下载pdf(根本没有压缩)时,我的代码工作正常。

1 个答案:

答案 0 :(得分:4)

使用此解压缩:

- (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];

}

并使用此方法从目录中获取pdf:

-(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;
}