VS&Qt compression and decompression

Preface: This blog post records the configuration of VS and the method of compression and decompression when VS-Qt is used for compression and decompression, for the reference of other small partners.

1. Add the GUI-Private module, which contains Qt's compression and decompression methods.

        First add the module in the property page - Qt Project Settings

 Second, add the Include file in the VC++ directory

C:\Qt\5.15.2\msvc2019_64\include\QtGui\5.15.2\QtGui;

C:\Qt\5.15.2\msvc2019_64\include\QtCore\5.15.2\QtCore;

2. Compression method

bool compressData(QFileInfoList fileList, QString basePath, QString SavePath)
{
    if (fileList.size() < 1) return false;

    QZipWriter* writer = new QZipWriter(SavePath);
    for(int i=0; i<fileList.size(); i++)
    {
        QFileInfo info = fileList.at(i);
        QString strFileName = info.fileName();
        if (info.fileName() == "." || info.fileName() == "..")
            continue;
        if (info.isFile())
        {
            QFile upfile(info.filePath());
            upfile.open(QIODevice::ReadOnly);
            QString fileName = info.filePath().mid(basePath.size(), info.filePath().size());
            writer->addFile(fileName, upfile.readAll());
        }
        else if (info.isDir())
        {
            QZipWriterEx(writer, info.filePath(), basePath);
        }
    }
    writer->close();
    delete writer;
    return  true;
}

bool QZipWriterEx(QZipWriter* writer, QString tmpPath, QString basePath)
{
    QDir dir(tmpPath);
    foreach(QFileInfo info, dir.entryInfoList())
    {
        if (info.fileName() == "." || info.fileName() == "..")
            continue;
        if (info.isFile())
        {
            QFile upfile(info.filePath());
            upfile.open(QIODevice::ReadOnly);
            std::cout << info.filePath().toStdString() << basePath.size() <<","<< info.filePath().size() << std::endl;
            QString fileName = info.filePath().mid(basePath.size(), info.filePath().size());
            writer->addFile(fileName, upfile.readAll());
            //qDebug() << fileName << tmpPath << basePath;
            upfile.close();
        }
        else if (info.isDir())
        {
            QZipWriterEx(writer, info.filePath(), basePath);
        }
    }
    return true;
}

3. Decompression method

bool decompressData(QString filepath, QString savepath)
{
    QDir dir(savepath);
    if (!dir.exists())
    {
        dir.mkpath(savepath);
    }
    bool unzipok = false;
    QZipReader zipreader(filepath);
    unzipok = zipreader.extractAll(savepath);   //可加可不加没有什么影响

    for (int i = 0; i < zipreader.fileInfoList().size(); ++i) {
        QStringList paths = zipreader.fileInfoList().at(i).filePath.split("/");
        paths.removeLast();
        QString path = paths.join("/");
        QDir subdir(savepath + "/" + path);
        if (!subdir.exists())
            dir.mkpath(QString::fromLocal8Bit("%1").arg(savepath + "/" + path));

        QFile file(savepath + "/" + zipreader.fileInfoList().at(i).filePath);
        file.open(QIODevice::WriteOnly);
        //注意编码问题
        QByteArray dt = zipreader.fileInfoList().at(i).filePath.toUtf8();
        QString strtemp = QString::fromLocal8Bit(dt);

        QByteArray array = zipreader.fileData(strtemp);
        file.write(array);
        file.close();
    }
    zipreader.close();
    return unzipok;
}

Guess you like

Origin blog.csdn.net/qq_40041064/article/details/130362515