模仿Qt的安裝界面,基於quazip解壓

能夠將放在QT的資源文件中,在解壓本地 .或者先複製到本地再解壓,支持多線程.

核心代碼:

void UncompressThread::uncompressFile(QString file,QString path)
{
    QString m_strAppPath =path;
    //QString strInFileName /*= "D:\\QtCore\\Uncompress\\release\\Qt5.7-static"*/;

    QStringList OutFileList;
    OutFileList<< file;
    qDebug()<<OutFileList;

    for(int i = 0;i < OutFileList.count();i++)
    {

        //SubFunExtractFile(strInFileName, OutFileList.at(i));


        bool bRet = SubFunExtractDir(OutFileList.at(i), m_strAppPath);
        if (bRet != true)
        {
            SendUncompressInfomationSig(QString::fromLocal8Bit("提取文件錯誤."));
        }
        SendUncompressInfomationSig(QString::fromLocal8Bit("提取文件完成."));
    }

    emit WorkerThreadFinishSig();
}


bool UncompressThread::SubFunExtractFile(QString strInFile, QString strOutFile)
{
    QFile infile(strInFile);
    if (!infile.open(QIODevice::ReadOnly))
    {
        return(false);
    }
    QByteArray data = infile.readAll();
    QFile outfile(strOutFile);
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        return(false);
    }
    outfile.write(data);
    infile.close();
    outfile.close();
    return(true);
}

bool UncompressThread::SubFunExtractDir(QString strInFile, QString strOutPath)
{
    QuaZip zipInFile(strInFile);
    if (!zipInFile.open(QuaZip::mdUnzip))
    {
        qDebug()<<"zipInFile open failed";
        return(false);
    }

    int nFileCount = zipInFile.getEntriesCount();


    int nFileProgress = 0;
    bool hasFile = zipInFile.goToFirstFile();
    while(hasFile)
    {
        bool bRet = SubFubExtractZipFile(zipInFile.getZipName(), zipInFile.getCurrentFileName(), strOutPath);
        if (bRet != true)
        {
            qDebug()<<"SubFubExtractZipFile failed";
            return(false);
        }
        nFileProgress++;
        emit SendProgressValueSig(100*nFileProgress/nFileCount);
        bRet = zipInFile.goToNextFile();
        if (bRet != true)
            break;
    }
    return(true);
}

bool UncompressThread::SubFubExtractZipFile(QString strInFile, QString strInFilePath, QString strOutPath)
{
    QuaZipFile infile(strInFile, strInFilePath);
    if (!infile.open(QIODevice::ReadOnly))
    {
        return(false);
    }
    QByteArray data = infile.readAll();
    QString outPath = strOutPath +'\\'+ strInFilePath;
    if (outPath.endsWith("/") == true)
    {
        QDir appDir(outPath);
        if (appDir.exists() != true)
        {
            appDir.mkpath(outPath);
        }
        return(true);
    }
    outPath.remove('\\');

    QFile outfile(outPath);
    QString file = outPath.remove(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0]);
    emit SendCurrentFileSig(file);
    SendUncompressInfomationSig(QString::fromLocal8Bit("正在提取文件")+file);
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        return(false);
    }
    outfile.write(data);
    infile.close();
    outfile.close();
    return(true);
}

需要的quazip已編譯打包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章