QT development-file operation (continuous update)

1. Get all the files in the folder

#include <QDir>
QStringList getFile(const QString &path)
{
    
    
    QStringList ret;
    QDir dir(path);
    //获取文件夹下所有的文件与文件夹
    QFileInfoList infolist = dir.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
    for(int i = 0;i<infolist.count();++i)
    {
    
    
        QFileInfo info = infolist.at(i);
        if(info.isDir())
        {
    
    
            QString subDir = info.absoluteFilePath();
            QStringList files = getFile(subDir);
            ret.append(files);
        }
        else
        {
    
    
            QString fileName = info.absoluteFilePath();
            ret.append(fileName);
        }
    }
    return ret;
}

Two, get the md5 of the file

Baidu Encyclopedia:
MD5 is the file signature bai, which is equivalent to our ID card unique. du
MD5 is often used on forums and software releases. It is a verification system designed to ensure the correctness of files and prevent some people from embezzling programs, adding some Trojan horses or tampering with copyright.

Each file can use the MD5 verification program to calculate a fixed MD5 code. Software authors often calculate the MD5 code of his program in advance and post it on the Internet.

Therefore, when you see the MD5 code next to a program download on the Internet, you can write it down. After downloading the program, use the MD5 verification program to calculate the MD5 code of the file you downloaded, and write down the MD5 code before. Compare.

If the two are the same, then you are downloading the original version. If the calculated value does not match the one indicated on the Internet, then the file you downloaded is incomplete or has been manipulated by others.

#include <QCryptographicHash>
QByteArray getFileMd5(const QString &fileName)
{
    
    
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly))
    {
    
    
        QCryptographicHash hash(QCryptographicHash::Md5);
        while(!file.atEnd())
        {
    
    
            //读取100m
            QByteArray content = file.read(100*1024*1024);
            hash.addData(content);
        }
        QByteArray md5 = hash.result().toHex();
        file.close();
        return md5;
    }
    return QByteArray();
}

Three, read the content of the txt file

void readFile(const QString &fileName)
{
    
    
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly))
    {
    
    
        QByteArray content = file.readAll();
        qDebug()<<content;
        file.close();
    }
}

doing…

Guess you like

Origin blog.csdn.net/peixin_huang/article/details/107361080
Recommended