zoukankan      html  css  js  c++  java
  • 运用MD5文件查重

    我们已经了解MD5是什么了。

    我们可以运用MD5查找重复文件。

    QByteArray Widget::getfileMD5(const QString &fileName)
    {
        QFile file(fileName);
        if(file.open(QIODevice::ReadOnly))
        {
            QCryptographicHash hash(QCryptographicHash::Md5);
            //按大小读取100M
            while(!file.atEnd())
            {
                 QByteArray content = file.read(100*1024*1024);
                 hash.addData(content);
            }
           QByteArray MD5 = hash.result();
           //qDebug()<<"The MD5 of file is "<<MD5.toHex();
            file.close();
           return MD5;
        }
    
    
        else
        {
            return QByteArray();
        }
    }
    QStringList files = getfile("C:/Users/DELL/Desktop/current study/testfilename");
    
    
        for(int i = 0; i<files.count();i++)
        {
            QString fileName = files.at(i);
            QByteArray md5 = getfileMD5(fileName);
            //qDebug()<<"filename为"<<fileName <<"MD5的值为"<< md5.toHex();
            fileMD5[md5].append(fileName);
        //qDebug()<<filelist;
        }
        //遍历fileMD5
        for(QHash<QByteArray,QStringList>::iterator it= fileMD5.begin();it!=fileMD5.end();it++)
        {
            qDebug()<<"MD5的值"<<(*it).first()<<"有多少个相同文件名count"<<it.value().count();
            if(it.value().count()>1)
            {
                qDebug()<<it.value();
            }
        }
    在遍历QHsh时,在QT中,这个就表示了键值对了,
    QHash<QByteArray,QStringList> fileMD5;
    在C++时,我们学过,<>中是2个类型,我们这个是QByteArray与QStringList两个类型。
    在QT中可以直接it.key()与it.value();访问。


  • 相关阅读:
    ubuntu1604安装微信
    python中汉字 hash值
    python-json
    lvm 扩展目录大小
    python数据可视化编程实战
    python-数据分析
    爬虫学习路径
    415. 字符串相加-字符串-简单
    43. 字符串相乘-字符串-中等难度
    44. 通配符匹配-动态规划-困难
  • 原文地址:https://www.cnblogs.com/qq376142178/p/12392878.html
Copyright © 2011-2022 走看看