简体   繁体   English

如何从NSData对象保存多个文件?

[英]How can I save multiple files from an NSData object?

So I have a zip file, which gets extracted using Objective-Zip. 所以我有一个zip文件,可以使用Objective-Zip提取该文件。 The extracted contents then get put into an NSData object. 然后将提取的内容放入NSData对象。 I want to save the contents into the apps Documents folder. 我想将内容保存到应用程序的“文档”文件夹中。 The problem is these are multiple files, and writeToFile:atomically: saves the contents of the zip into one file. 问题是这些是多个文件,而writeToFile:atomically:将zip的内容保存到一个文件中。

Any ideas? 有任何想法吗? The other question I saw just said to use ZipArchive. 我刚刚看到的另一个问题是使用ZipArchive。

I need help fast, because the app needs to be finished by the end of my work tomorrow. 我需要快速帮助,因为该应用程序需要在明天工作结束之前完成。

I think you need to use ZipArchive: 我认为您需要使用ZipArchive:

ZipArchive* za = [[ZipArchive alloc] init];
NSData *Data = [NSData  dataWithContentsOfURL:url]; // here url is your zip url
NSArray *documentPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirString = [documentPathArray objectAtIndex:0];
NSString *filePath = [documentsDirString stringByAppendingPathComponent:@"temp.zip"];

NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
[Data writeToURL: fileURL options:0 error:&writeError];
if( writeError) {
   NSLog(@" Error in writing file %@' : \n %@ ", filePath , writeError );
   return;
}
if( [za UnzipOpenFile:filePath] )
{

    BOOL ret = [za UnzipFileTo:documentsDirz overWrite:YES];
    if( NO==ret )
     {
      NSLog(@"NO");
    }
    [za UnzipCloseFile];
} 

for more info 了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM