【发布时间】: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