支持大文件md5的获取
废话不说,直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <QString> #include <QByteArray> #include <QCryptographicHash> #include <QFile> #include <QDebug> QByteArray getFileMd5(QString filePath) { QFile localFile(filePath); if (!localFile.open(QFile::ReadOnly)) { qDebug() << "file open error."; return 0; } QCryptographicHash ch(QCryptographicHash::Md5); quint64 totalBytes = 0; quint64 bytesWritten = 0; quint64 bytesToWrite = 0; quint64 loadSize = 1024 * 4; QByteArray buf; totalBytes = localFile.size(); bytesToWrite = totalBytes; while (1) { if(bytesToWrite > 0) { buf = localFile.read(qMin(bytesToWrite, loadSize)); ch.addData(buf); bytesWritten += buf.length(); bytesToWrite -= buf.length(); buf.resize(0); } else { break; } if(bytesWritten == totalBytes) { break; } } localFile.close(); QByteArray md5 = ch.result(); return md5; } |
发表评论
要发表评论,您必须先登录。