3

我正在尝试清理一些现有的代码,这些代码从服务器中分块下载一个大文件,检查每 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)。谢谢!

4

1 回答 1

6

一次将这么多数据保存在内存中肯定是个问题。幸运的是,您不需要这样做——您可以在数据从网络中取出时将其写入磁盘,并且您可以保持运行中的校验和。

用你的 ReceivedData 换几个新的 ivars:

NSFileHandle* filehandle;
MD5_CTX md5sum;

MD5_CTX 在 OpenSSL 中,而……iOS 上还没有?嘎。好的,您可以在线找到 MD5 源,例如:http : //people.csail.mit.edu/rivest/Md5.c你不需要。但如果你碰巧已经在使用 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 和,并且您几乎不会使用任何内存。

于 2012-05-08T16:52:53.300 回答