zoukankan      html  css  js  c++  java
  • iOS开发,利用文件流,算大文件的MD5值(程序不会导致内存崩溃)

    CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath,
                                           size_t chunkSizeForReadingData) {
        
        // Declare needed variables
        CFStringRef result = NULL;
        CFReadStreamRef readStream = NULL;
        
        // Get the file URL
        CFURLRef fileURL =
        CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                      (CFStringRef)filePath,
                                      kCFURLPOSIXPathStyle,
                                      (Boolean)false);
        if (!fileURL) goto done;
        
        // Create and open the read stream
        readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
                                                (CFURLRef)fileURL);
        if (!readStream) goto done;
        bool didSucceed = (bool)CFReadStreamOpen(readStream);
        if (!didSucceed) goto done;
        
        // Initialize the hash object
        CC_MD5_CTX hashObject;
        CC_MD5_Init(&hashObject);
        
        // Make sure chunkSizeForReadingData is valid
        if (!chunkSizeForReadingData) {
            chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
        }
        
        // Feed the data to the hash object
        bool hasMoreData = true;
        while (hasMoreData) {
            uint8_t buffer[chunkSizeForReadingData];
            CFIndex readBytesCount = CFReadStreamRead(readStream,
                                                      (UInt8 *)buffer,
                                                      (CFIndex)sizeof(buffer));
            if (readBytesCount == -1) break;
            if (readBytesCount == 0) {
                hasMoreData = false;
                continue;
            }
            CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount);
        }
        
        // Check if the read operation succeeded
        didSucceed = !hasMoreData;
        
        // Compute the hash digest
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5_Final(digest, &hashObject);
        
        // Abort if the read operation failed
        if (!didSucceed) goto done;
        
        // Compute the string result
        char hash[2 * sizeof(digest) + 1];
        for (size_t i = 0; i < sizeof(digest); ++i) {
            snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
        }
        result = CFStringCreateWithCString(kCFAllocatorDefault,
                                           (const char *)hash,
                                           kCFStringEncodingUTF8);
        
    done:
        
        if (readStream) {
            CFReadStreamClose(readStream);
            CFRelease(readStream);
        }
        if (fileURL) {
            CFRelease(fileURL);
        }
        return result;
    }
    
    +(NSString*)fileMD5:(NSString*)path
    {
        return (__bridge_transfer NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
    }
  • 相关阅读:
    eclipse中,把java函数代码折叠/展开 介绍【转】
    苹果开发者账号注册&真机调试
    Objective-C编码规范:26个方面解决iOS开发问题
    iTunes获取下载的安装包
    Mac AppStore下载文件的获取
    Mac 切换Windows 使用虚拟机, 不推荐双系统
    Xcode使用版本
    如何提高代码质量
    ARC的内存管理
    Objective-C 类的继承、方法的重写和重载
  • 原文地址:https://www.cnblogs.com/visen-0/p/3160907.html
Copyright © 2011-2022 走看看