Iphone 下载nsDocumentDirectory中的zip文件,取消归档并读取它

Iphone 下载nsDocumentDirectory中的zip文件,取消归档并读取它,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,我在URL上有一个.zip文件,我想下载它,取消归档并将其保存到nsdocument目录中,无需任何UI或用户交互。然后读回。在.h文件中声明此变量 NSMutableData *responseData; 在viewDidLoad方法中编写此代码 此方法将开始从给定URL下载文件 NSURL *serverURL = [NSURL URLWithString:@"your file URL here"]; NSURLRequest *request = [NSURLRequest reque

我在URL上有一个.zip文件,我想下载它,取消归档并将其保存到nsdocument目录中,无需任何UI或用户交互。然后读回。

在.h文件中声明此变量

NSMutableData *responseData;
在viewDidLoad方法中编写此代码 此方法将开始从给定URL下载文件

NSURL *serverURL = [NSURL URLWithString:@"your file URL here"];
NSURLRequest *request = [NSURLRequest requestWithURL:serverURL];
NSURLConnection *cn = [NSURLConnection connectionWithRequest:request delegate:self];
[cn start];
在.m文件中实现此委托方法

#pragma mark - NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%s",__FUNCTION__);
    responseData = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"%s",__FUNCTION__);
    responseData = [[NSMutableData alloc] initWithCapacity:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"%s",__FUNCTION__);
    [responseData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"%s",__FUNCTION__);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"DownloadedZip.zip"];

    // Save file to Document Directory
    [responseData writeToFile:filePath atomically:YES];
    responseData = nil;
}

将项目下载到无存档zip文件,并在将文件保存到文档目录后放入无存档代码

首先将文件下载到您的设备

NSString *URLstring = @"http://your.download.URL";
NSURL  *url = [NSURL URLWithString:URLstring];
NSData *urlData = [NSData dataWithContentsOfURL:url];
然后将其写入设备。如果路径是设备上要保存它的位置以及要保存它的文件名,请记住添加文件扩展名(.zip)

然后你可以使用类似或的东西来解压它。有几种开源解决方案。这两个人过去曾为我工作过。很容易使用

在这之后,对解压后的数据执行任何操作都非常简单。然后做一些室内清洁,并从手机中删除zip文件。

试试看
[urlData writeToFile:thePath atomically:YES];