在iOS应用中下载大文件

时间:2012-05-08 15:41:27

标签: iphone web-services nsurlconnection nsdata nsurl

我正在尝试清理一些现有代码,这些代码从服务器中以块的形式下载大文件,检查每个50个数据包的校验和,然后将它们拼接在一起。我有一些麻烦,看看它是否是最有效的方式,因为内存问题,它现在崩溃了一段时间。如果我没有校验和,它似乎没有崩溃,但我希望我能先检查我的文件。

@property (nonatomic, retain) NSMutableData * ReceivedData;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSData *sequencePacketData = [[NSData alloc] initWithData:self.ReceivedData]; 
        [self ProcessPacket:sequencePacketData];
        [sequencePacketData release];
        [[NSNotificationCenter defaultCenter] postNotificationName:DownloadNotification object:self];

}

- (void)ProcessPacket:(NSData *)sequencePacketData {
  // find the directory I need to write to and the name of the file
NSString *currentChecksum = [WebServiceManager MD5CheckSumForNSData:sequencePacketData];
    BOOL checkSumValid = [dmgr ValidateChecksum:currentChecksum againstFileName:self.CurrentFileName];
    self.IsSuccessful = checkSumValid;

    if (!checkSumValid) {
        // log error msg
        return;
    }

    if (success)
    {
        NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:sequencePath];
        [handle seekToEndOfFile];
        [handle writeData:sequencePacketData];
        [handle closeFile];
    }
    else
    {
        [sequencePacketData writeToFile:sequencePath atomically:YES];
    }

    // When file is completely downloaded, check the checksum of the entire file:
BOOL completeFileCheckSum;
    if ([packetFile isEqualToString:@"50.bin"]) {
        NSData *temData = [NSData dataWithContentsOfFile:sequencePath];
        currentChecksum = [WebServiceManager MD5CheckSumForNSData:temData];
        completeFileCheckSum = [dmgr ValidateChecksum:currentChecksum againstFileName:fileName];
        NSLog(@"Checksum for whole file is valid: %i", completeFileCheckSum);
        if (!completeFileCheckSum) {
            NSError *err;
            [fileManager removeItemAtPath:sequencePath error:&err];

            // log error
            return;
        }
    }
}

+ (NSString*)MD5CheckSumForNSData:(NSData *) input
{
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

    // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(input.bytes, input.length, md5Buffer);

    // Convert unsigned char buffer to NSString of hex values
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) 
        [output appendFormat:@"%02x",md5Buffer[i]];
    return output;
}

check checksum againstFile方法只是从临时文件中获取校验和并进行比较。

我读到了关于NSAutoReleasePools以及如果你正在加载一堆图像并且需要清除内存等,这有什么用呢,但是我不确定这是否真的适用于此,如果那样,或者其他什么可以帮助下载一个大文件(略小于1 GB)。谢谢!

1 个答案:

答案 0 :(得分:6)

一次将大量数据保存在内存中肯定会成为一个问题。幸运的是,您不需要 - 您可以将数据写入磁盘,并且可以保持运行的校验和。

将您的ReceivedData换成几个新的ivars:

NSFileHandle* filehandle;
MD5_CTX md5sum;

MD5_CTX在OpenSSL中,哪个......仍然不在iOS上?嘎。好的,您可以在线找到MD5源,例如这里:http://people.csail.mit.edu/rivest/Md5.c(我原本建议将OpenSSL添加到你的项目中,但这是你不需要的大量额外垃圾。但如果你碰巧已经在使用OpenSSL,那么它包含了MD5功能。)

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    MD5Init(&md5sum);

    filehandle = [[NSFileHandle filehandleForWritingAtPath:path] retain];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    MD5Update(&md5sum, [data bytes], [data length]);

    [filehandle writeData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    MD5Final(&md5sum);
    // MD5 sum is in md5sum.digest[]

    [filehandle closeFile];

    // verify MD5 sum, etc..
}

最后,你的文件将在磁盘上,你将拥有它的MD5总和,你几乎不会使用任何内存。