我想从服务器下载一个大型的zip文件,最高可达50 mb。所以任何人都可以建议我如何下载大型zip文件。
我已经实现了下载普通zip文件的代码,但我想下载大文件。
答案 0 :(得分:1)
下载任何大小的文件时应该没有问题。结帐NSURLConnection
:
答案 1 :(得分:1)
直接从项目中获取:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
bytesCount = bytesCount + [data length];
[receivedData appendData:data];
//If the size is over 10MB, then write the current data object to a file and clear the data
if(receivedData.length > MAX_DATA_LENGHT){
[fileHandle truncateFileAtOffset:[fileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file
[fileHandle writeData:receivedData]; //actually write the data
[receivedData release];
receivedData = nil;
receivedData = [[NSMutableData data] retain];
}
[progressView setProgress:(float)bytesCount/sizeOfDownload];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
DEBUG(@"Succeeded! Received %d bytes of data",[receivedData length]);
// Release and clean some ivars
//
[currentConnection release];
currentConnection = nil;
[fileHandle writeData:receivedData];
[receivedData release];
[fileHandle release];
..
这是开始下载例程的一部分:
...
// create a path in doc's folder and initialize the file handler
NSString *temporaryZipPath = [self temporaryZipPathForResource];
NSMutableData *fake = [[NSMutableData alloc] initWithLength:0];
BOOL result = [[NSFileManager defaultManager] createFileAtPath:temporaryZipPath
contents:fake
attributes:nil];
[fake release];
if (!result) {
[super showAlertWithErrorDescription:@"Error creating file"];
return;
}
//
fileHandle = [[NSFileHandle fileHandleForWritingAtPath:temporaryZipPath] retain];
//
NSURLRequest *request = [NSURLRequest requestWithURL:[self audioPackageURL]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0f];
currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
receivedData = [[NSMutableData data] retain];
...
希望它有所帮助。