iphone - 如何将下载的文件解压到文档目录

标签 iphone objective-c ipad zip

我搜索解压缩文档目录中下载的 .zip 文件。但我只发现了有关它们的问题,但没有得到适合我的查询的任何合适答案。

每个人都建议下载一些名为“MiniZip”的 api 文件并加以利用。但是它的代码很笨重,而且我不需要那么多代码。所以,如果我能得到一些更少的代码来解压缩文件并使用它,那对我来说会很棒。它是从 url 下载的,与存储时完全一样,但我不知道如何在文档目录中解压缩和使用它。任何人都可以通过提供一些示例代码或建议我来帮助我..

以下代码是使用 url 下载我的 zip 文件的代码。

-(IBAction)download:(id)sender{

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://some url contains .zip file"]  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];                                                                        
    NSURLConnection  *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Inform the user that the download failed.
        recievedData=[[NSMutableData data ]retain];

    //  [recievedData writeToFile:path atomically:YES];
        NSLog(@"download ");
    }
    else {
        NSLog(@"download fail");
    }
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

        [recievedData setLength:0];
    }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [recievedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
        [connection release];

    [recievedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[recievedData length]);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"books"];
    NSLog(@"value of the path is:%@",path);
    [recievedData writeToFile:[path stringByAppendingPathComponent:@"file"] atomically:YES];

    [connection release];
    [recievedData release];
}   

最佳答案

MiniZip 是否给您带来任何性能问题?

如果没有,请查看 objective-zip .

关于iphone - 如何将下载的文件解压到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4548256/

相关文章:

iphone - 如何使用 Quartz 2D 向 UIImage 或 UIImageView 添加阴影?

ios - 如何根据父 View 调整 subview 的大小和位置?

iphone - 无法控制 UITextView 的宽度

objective-c - 奇怪的 NSData 输出

ios - xcode 核心数据类中的数据类型不匹配

objective-c - 我可以将委托(delegate)作为参数传递吗?

c# - 选择行后弹出框不消失

objective-c - 从不同着色器导入函数

iOS 7 SDK - 导航 Controller 中的 ScrollView 在模拟器和设备上不同

ios - 无法在某些应用程序中选择日文键盘