【问题标题】:How to unzip the downloaded file in the document directory如何将下载的文件解压到文档目录
【发布时间】:2010-12-28 18:11:03
【问题描述】:

我搜索解压文件目录中下载的 .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];
}   

【问题讨论】:

    标签: iphone objective-c ipad zip


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,Altealice,我也尝试过使用 minizip,但它没有给我任何结果.. 我什至找不到我遇到这个问题的地方。你能建议我用其他方法来克服它吗?
    • 我的队友使用了 MiniZip,它在我们的项目中运行良好。我不知道他是如何使用它的具体细节。也许您应该尝试阅读更多文档。
    • 很高兴知道!祝你的项目好运。 :)
    • 项目似乎已停止。这篇文章现在大约有 3 年的历史了。试试这个,仍然基于 MiniZip。 github.com/samsoffes/ssziparchive
    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多