qt使用wimlib-imagex,做windows系统备份还原

news2024/9/25 23:20:16

wimlib-imagex是个第三方工具,可对系统映像进行操作,下载地址:
https://wimlib.net/downloads/index.html
在这里插入图片描述
程序主要用到以下这两个文件:libwim-15.dll和wimlib-imagex.exe
在这里插入图片描述
wimlib-imagex.exe的调用命令参数,可以通过cmd命令行wimlib-imagex --help进行查看,也可以自行百度。




系统备份:通过wimlib-imagex的capture命令,对指定系统盘进行捕获映像,生成wim文件。
系统还原:进入winPE系统,对指定系统盘进行格式化,再通过wimlib-imagex的apply命令,将之前备份的完整的wim文件应用到系统盘。


以下为涉及的部分参考代码:

SystemReloadObject.cpp


//系统备份
bool CSystemReloadObject::systemBackup(const QString& backupName, const QString& backupDesc)
{
    QString sourcePath = getSystemDrive();

    m_pThread->systemBackup(sourcePath, backupName, backupDesc);
    return true;
}

//系统还原
bool CSystemReloadObject::systemRestore(const QString& backupName, const int& index)
{
    m_pThread->systemRestore(backupName, index);
    return true;
}


//本地模式下载安装
bool CSystemReloadObject::systemPEDownload()
{
    m_pThread->systemPEDownload();
    return true;
}


//获取当前系统盘盘符
QString CSystemReloadObject::getSystemDrive()
{
    char str[MAX_PATH]{0};
    GetSystemDirectoryA(str, MAX_PATH);
    return QString::fromStdString(std::string(1, str[0]));
}


//获取当前系统版本
QString CSystemReloadObject::getSystemVersion()
{
    QString vname;
    //先判断是否为win8.1或win10
    typedef void(__stdcall* NTPROC)(DWORD*, DWORD*, DWORD*);
    HINSTANCE hinst = LoadLibrary(L"ntdll.dll");
    DWORD dwMajor, dwMinor, dwBuildNumber;
    NTPROC proc = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers");
    proc(&dwMajor, &dwMinor, &dwBuildNumber);
    if (dwMajor == 6 && dwMinor == 3)	//win 8.1
    {
        vname = "Microsoft Windows 8.1";
        return vname;
    }
    if (dwMajor == 10 && dwMinor == 0)	//win 10
    {
        vname = "Microsoft Windows 10";
        return vname;
    }

    //判断win8.1以下的版本
    SYSTEM_INFO info;                //用SYSTEM_INFO结构判断64位AMD处理器
    GetSystemInfo(&info);            //调用GetSystemInfo函数填充结构
    OSVERSIONINFOEX os;
    os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(disable:4996)
    int SystemVer = 0;
    if (GetVersionEx((OSVERSIONINFO*)&os))
    {

        //下面根据版本信息判断操作系统名称
        switch (os.dwMajorVersion)
        {                        //判断主版本号
        case 4:
            switch (os.dwMinorVersion)
            {                //判断次版本号
            case 0:
                if (os.dwPlatformId == VER_PLATFORM_WIN32_NT) {
                    vname = "Microsoft Windows NT 4.0";  //1996年7月发布
                    SystemVer = 40; // Windows NT 4.0
                }
                else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
                    vname = "Microsoft Windows 95";
                    SystemVer = 95;
                }

                break;
            case 10:
                vname = "Microsoft Windows 98";
                SystemVer = 41; // Windows 98
                break;
            case 90:
                vname = "Microsoft Windows Me";
                SystemVer = 42; // Windows Me
                break;
            }
            break;
        case 5:
            switch (os.dwMinorVersion)
            {               //再比较dwMinorVersion的值
            case 0:
                vname = "Microsoft Windows 2000";    //1999年12月发布
                SystemVer = 50; // Windows 2000
                break;
            case 1:
                vname = "Microsoft Windows XP";      //2001年8月发布
                SystemVer = 51; // Windows XP
                break;
            case 2:
                if (os.wProductType == VER_NT_WORKSTATION &&
                    info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
                {
                    vname = "Microsoft Windows XP Professional x64 Edition";
                    SystemVer = 52;
                }
                else if (GetSystemMetrics(SM_SERVERR2) == 0)
                {
                    vname = "Microsoft Windows Server 2003";   //2003年3月发布
                    SystemVer = 53;
                }
                else if (GetSystemMetrics(SM_SERVERR2) != 0)
                {
                    vname = "Microsoft Windows Server 2003 R2";
                    SystemVer = 54;
                }

                break;
            }
            break;
        case 6:
            switch (os.dwMinorVersion)
            {
            case 0:
                if (os.wProductType == VER_NT_WORKSTATION)
                {
                    vname = "Microsoft Windows Vista";
                    SystemVer = 60; // Windows Vista
                }
                else {
                    vname = "Microsoft Windows Server 2008";   //服务器版本
                    SystemVer = 61; // Windows Server 2008
                }

                break;
            case 1:
                if (os.wProductType == VER_NT_WORKSTATION)
                {
                    vname = "Microsoft Windows 7";
                    SystemVer = 70; // Windows 7

                }
                else
                {
                    vname = "Microsoft Windows Server 2008 R2";
                    SystemVer = 71; // Windows Server 2008 R2
                }

                break;
            case 2:
                if (os.wProductType == VER_NT_WORKSTATION)
                {
                    vname = "Microsoft Windows 8";
                    SystemVer = 80; // Windows 8
                }

                else {
                    vname = "Microsoft Windows Server 2012";
                    SystemVer = 81; // Windows Server 2012
                }

                break;
            }
            break;
        default:
            vname = "未知操作系统";
            SystemVer = 0;
        }
    }
    else {
        printf_s("版本获取失败\n");
    }
    return vname;
}


//获取当前存在的备份
void CSystemReloadObject::getSystemBackup()
{
    QString backupPath = QDir::currentPath() + "/SystemBackup/";
    QDir dir(backupPath);
    QFileInfoList infolist = dir.entryInfoList();

    QJsonArray backupArray;
    foreach(auto it, infolist){
        if(it.suffix() == "wim" || it.suffix() == "WIM"){
            QJsonObject backupObj;
            backupObj.insert("name", it.absoluteFilePath());
            backupArray.append(backupObj);
        }
    }

    emit sigBackupInfo(backupArray);
}


//获取当前存在的有效备份的卷
void CSystemReloadObject::getSystemBackupImage(const QString& backupPath)
{
    //使用wimlib-imagex的info命令检验
    QString cmd = QString("\"%1/SystemReload/wimlib-imagex.exe\" info \"%2\"")
                      .arg(QDir::currentPath())
                      .arg(backupPath);

    QProcess process;
    process.start(cmd);
    process.waitForFinished();
    QByteArray all = process.readAll();
    QString strall = QString(all);
    int indexs = 0;

    if (!strall.contains("Available Images:")) {
        qDebug() << "此wim无效,无法进行还原";
    }else{
        qDebug() << "有可用的Images";

        int pos = strall.indexOf("Available Images:");
        strall = strall.mid(pos);
        indexs = strall.count("Index:");
    }

    //添加数据到model
    QJsonArray imageArray;
    for(int i=0; i<indexs; i++){
        QJsonObject imageObj;
        imageObj.insert("name", QString::number(i+1));
        imageArray.append(imageObj);
    }

    emit sigImageInfo(imageArray);
}


//检测当前系统是否是winPE环境
bool CSystemReloadObject::isCurrentWinPE()
{
    HKEY hkey = NULL;
    unsigned char buffer[260] = { 0 };
    unsigned long dwsize = sizeof(buffer);
    std::string strfind;

    unsigned long iret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control", 0, KEY_READ, &hkey);
    if (iret != ERROR_SUCCESS)
        return false;

    iret = RegQueryValueExA(hkey, "SystemStartOptions", NULL, NULL, buffer, &dwsize);
    if (iret != ERROR_SUCCESS)
    {
        RegCloseKey(hkey);
        return false;
    }

    RegCloseKey(hkey);// get registry keys.

    // next find '/MININT' flag.
    strfind.assign((char*)buffer);
    std::transform(strfind.begin(), strfind.end(), strfind.begin(), tolower);// to lowercase.
    if ((int)strfind.find("minint", 0) > -1){
        //是winPE
        emit sigDialogOperte(1);
        return true;
    }

    //不是winPE
    emit sigDialogOperte(2);
    return false;
}


//检查当前是否存在winPE
bool CSystemReloadObject::hasLocalWinPE()
{
    //判断系统BCD引导文件是否有winPE的引导菜单
    QString cmd = QString("bcdedit /enum");

    QStringList args;
    args << "/C" << cmd;

    QString program = "cmd.exe";

    QProcess process;
    process.start(program, args);
    process.waitForFinished();

    QByteArray all = process.readAll();
    QString strall = QString(all);
    if (strall.contains(LOCAL_WINPE_DEVICE_GUID) || strall.contains(LOCAL_WINPE_OSLOADER_GUID)) {
        qDebug() << "当前存在winpe本地模式";
        emit sigDialogOperte(3);
        return true;
    }

    emit sigDialogOperte(4);
    return false;
}




//打开备份文件位置
void CSystemReloadObject::openBackupFilePath()
{
    QString strPath = QDir::currentPath() + "/SystemBackup";
    strPath.replace("/", "\\");
    QProcess process;
    process.startDetached("explorer", QStringList() << QString("/select,") << QString("%1").arg(strPath));
}


//关闭线程启用的外部程序
void CSystemReloadObject::exitApp()
{
    m_pThread->killProcess();
}


//重启电脑
void CSystemReloadObject::restartComputer()
{
    QString cmd = "shutdown /r /t 0";

    QStringList args;
    args << "/C" << cmd;

    QString program = "cmd.exe";
    QProcess process;
    process.start(program, args);
    process.waitForFinished();
}


SystemReloadThread.cpp
void CSystemReloadThread::systemBackup(const QString& sourcePath, const QString& backupName, const QString& backupDesc)
{
    m_threadParams.clear();
    m_threadParams.append(sourcePath);
    m_threadParams.append(backupName);
    m_threadParams.append(backupDesc);

    m_memberFun = &CSystemReloadThread::systemBackup;
    this->start();
}

bool CSystemReloadThread::systemBackup(QVariantList params)
{
    //创建备份文件夹
    QDir tempDir(m_BackupFilePath);
    if (!tempDir.exists()) {
        if (!tempDir.mkpath(m_BackupFilePath)) {
            qDebug() << "failed to create backupDir";
            sigDialogResultThread(1,0);
            emit sigUpdateBtn(true);
            return false;
        }
    }

    QString filepath = m_BackupFilePath + params[1].toString() + ".wim";
    QFile wimFile(filepath);

    //不存在就采用捕获capture,存在就采用追加append
    if (!wimFile.exists()) {
        WimCapture(params[0].toString() + ":\\", m_BackupFilePath, params[1].toString(), params[2].toString()+"_"+getSystemDrive());
    }
    else {
        WimAppend(params[0].toString() + ":\\", m_BackupFilePath, params[1].toString(), params[2].toString()+"_"+getSystemDrive());
    }


    //判断进程结束后,是否已成功完成备份
    if ((m_DoingOutput.contains("Calculating integrity table for WIM") && m_DoingOutput.contains("(100%) done"))
        || (m_DoingOutput.contains("Archiving file data:") && m_DoingOutput.contains("(100%) done"))) {
        qDebug() << "备份成功完成";
        emit sigDialogResultThread(1,1);
        emit sigUpdateBtn(true);
        return true;
    }else if(m_DoingOutput.contains("ERROR: Exiting with error code")){
        qDebug()<< "系统备份失败";
        emit sigDialogResultThread(1,0);
        emit sigUpdateBtn(true);
        return false;
    }


    return false;
}


void CSystemReloadThread::systemRestore(const QString& backupName, const int& index)
{
    m_threadParams.clear();
    m_threadParams.append(backupName);
    m_threadParams.append(index);

    m_memberFun = &CSystemReloadThread::systemRestore;
    this->start();
}

bool CSystemReloadThread::systemRestore(QVariantList params)
{

    //winPE的系统盘可能和windows10下不同,因此此处需要从备份文件的描述中获取系统盘
    //使用wimlib-imagex的info命令检验
    QString cmd = QString("\"%1/SystemReload/wimlib-imagex.exe\" info \"%2\"")
          .arg(QDir::currentPath())
          .arg(params[0].toString());

    QProcess process;
    process.start(cmd);
    process.waitForFinished();
    QByteArray all = process.readAll();
    QString strall = QString(all);
    QString systemDrive;


    if (!strall.contains("Available Images:")) {
        qDebug() << "此wim无效,无法进行还原";
    }
    else {
        qDebug() << "有可用的Images";

        int pos = strall.indexOf("Description:");
        strall = strall.mid(pos);
        pos = strall.indexOf("\r\n");
        systemDrive = strall.mid(pos - 1, 1);
    }


    //在winPE环境中,先格式化系统盘
    if(FormatVolume(systemDrive, "NTFS")){
        qDebug()<< "格式化成功";
    }else{
        qDebug()<< "格式化失败";
        emit sigDialogResultThread(2,0);
        emit sigUpdateBtn(true);
        return false;
    }

    //将wim文件应用到系统盘
    WimApply(params[0].toString(), params[1].toInt(), systemDrive+":\\");
    if (m_DoingOutput.contains("Done applying WIM image") || (m_DoingOutput.contains("Applying metadata to files") && m_DoingOutput.contains("(100%) done"))) {
        qDebug() << "还原成功完成";
        emit sigDialogResultThread(2,1);
        emit sigUpdateBtn(true);
        return true;
    }else if(m_DoingOutput.contains("ERROR: Exiting with error code")){
        qDebug()<< "系统还原失败";
        emit sigDialogResultThread(2,0);
        emit sigUpdateBtn(true);
        return false;
    }

    return false;
}


void CSystemReloadThread::systemPEDownload()
{
    m_threadParams.clear();
    m_memberFun = &CSystemReloadThread::systemPEDownload;
    this->start();
}


bool CSystemReloadThread::systemPEDownload(QVariantList params)
{
    emit sigDialogResultThread(3,0);
    //创建下载文件夹
    QDir tempDir(m_DownloadFilePath);
    if (!tempDir.exists()) {
        if (!tempDir.mkpath(m_DownloadFilePath)) {
            qDebug() << "failed to create downDir";
            return false;
        }
    }


    //下载文件
    m_DownloadFlag = true;
    downloadFile(m_DownloadMd5, m_DownloadUrl, m_DownloadFilePath, "");
    if (!m_DownloadFlag) {
        qDebug() << "download error";
        emit sigDialogResultThread(3,1);
        return false;
    }


    //解压文件
    std::string strDownloadInfo = m_DownloadFilePath.toStdString() + "winPE_X64_local.zip";
    std::string strTempDirPath = m_DownloadFilePath.toStdString();

    unZip(strDownloadInfo.c_str(), strTempDirPath.c_str());
    DeleteFileA(strDownloadInfo.c_str());



    //本地模式,使用bcdedit.exe 编辑BCD引导文件,进行winPE的菜单添加
    QString systemDrive = getSystemDrive();
    QString cmd = QString("bcdedit /create {%1} /d %2 /device && "
                          "bcdedit /create  {%3} /d %4 /application osloader && "
                          "bcdedit /set {%5} ramdisksdidevice partition=%6: && "
                          "bcdedit /set {%7} ramdisksdipath %8 && "
                          "bcdedit /set {%9} device ramdisk=[%10:]%11,{%12} && "
                          "bcdedit /set {%13} description %14 && "
                          "bcdedit /set {%15} locale zh-CN && "
                          "bcdedit /set {%16} inherit {bootloadersettings} && "
                          "bcdedit /set {%17} osdevice ramdisk=[%18:]%19,{%20} && "
                          "bcdedit /set {%21} systemroot \\windows && "
                          "bcdedit /set {%22} detecthal Yes && "
                          "bcdedit /set {%23} winpe Yes && "
                          "bcdedit /set {%24} ems no && "
                          "bcdedit /displayorder {%25} /addlast && "
                          "bcdedit /timeout %26 && "
                          "bcdedit /default {%27}")
                      .arg(LOCAL_WINPE_DEVICE_GUID).arg(LOCAL_WINPE_DISPNAME)
                      .arg(LOCAL_WINPE_OSLOADER_GUID).arg(LOCAL_WINPE_DISPNAME)
                      .arg(LOCAL_WINPE_DEVICE_GUID).arg(systemDrive)
                      .arg(LOCAL_WINPE_DEVICE_GUID).arg(LOCAL_WINPE_BOOT_SDI)
                      .arg(LOCAL_WINPE_OSLOADER_GUID).arg(systemDrive).arg(LOCAL_WINPE_BOOT_WIM).arg(LOCAL_WINPE_DEVICE_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID).arg(LOCAL_WINPE_DISPNAME)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID).arg(systemDrive).arg(LOCAL_WINPE_BOOT_WIM).arg(LOCAL_WINPE_DEVICE_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(LOCAL_WINPE_OSLOADER_GUID)
                      .arg(10)
                      .arg(LOCAL_WINPE_OSLOADER_GUID);



    QStringList args;
    args << "/C" << cmd;

    QString program = "cmd.exe";
    QProcess process;
    process.start(program, args);
    process.waitForFinished();


    //移动到系统盘指定文件夹
    QString i4path = systemDrive + ":/i4";
    QDir i4Dir(i4path);
    if (!i4Dir.exists()) {
        if (!i4Dir.mkpath(i4path))
            qDebug() << "failed to create dir";
    }

    QString i4sourcepath = QDir::currentPath() + "/SystemBackup/temp/winPE_X64_local";
    CopyDir(i4sourcepath, i4path);


    //删除文件夹
    QDir(i4sourcepath).removeRecursively();

    emit sigDialogResultThread(3,2);
    return false;
}



//将某个盘捕获到指定目录,生成wim文件
//WimCapture("c:\\", "e:\\", "c_bak", "c盘备份");
void CSystemReloadThread::WimCapture(const QString& sourcePath, const QString& targetPath, const QString& backupName, const QString& backupDescription)
{
    //wimlib-imagex capture C:\ D:\C_Bak.wim C_bak C盘备份 --check --snapshot
    QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" capture %2 \"%3\" \"%4\" \"%5\" --compress=XPRESS --threads=4 --snapshot")
    //QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" capture %2 \"%3\" \"%4\" \"%5\" --check --snapshot")
                              .arg(QDir::currentPath())
                              .arg(sourcePath)
                              .arg(targetPath + backupName +".wim")
                              .arg(backupName)
                              .arg(backupDescription);

    runCMD(commandLine.toStdString());
}



//追加到某个wim文件
//WimAppend("c:\\", "e:\\", "c_bak", "c盘备份");
void CSystemReloadThread::WimAppend(const QString& sourcePath, const QString& targetPath, const QString& backupName, const QString& backupDescription)
{
    //wimlib-imagex append c:\ d:\c_bak.wim c_bak_2 c盘备份
    //追加的话,参数%4(备份名字(不是备份文件))不能和之前相同
    QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" append %2 \"%3\" \"%4\" \"%5\" --compress=XPRESS --threads=4 --snapshot")
    //QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" append %2 \"%3\" \"%4\" \"%5\" --check --snapshot")
                              .arg(QDir::currentPath())
                              .arg(sourcePath)
                              .arg(targetPath + backupName + ".wim")
                              .arg(backupName +"_"+ QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
                              .arg(backupDescription);

    runCMD(commandLine.toStdString());
}


//将wim文件应用到某个盘
//WimApply("e:\\c_bak.wim", 1, "c:\\");
void CSystemReloadThread::WimApply(const QString& backupPath, const int& index, const QString& targetPath)
{
    //wimlib-imagex apply d:\c_bak.wim 1 c:\

    QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" apply \"%2\" %3 %4")
                              .arg(QDir::currentPath())
                              .arg(backupPath)
                              .arg(index)
                              .arg(targetPath);

    runCMD(commandLine.toStdString());
}


//检查wim文件信息
//WimInfo("e:\\c_bak.wim");
void CSystemReloadThread::WimInfo(const QString& backupPath)
{
    //wimlib-imagex info d:\c_bak.wim
    QString commandLine = QString("\"%1/SystemReload/wimlib-imagex.exe\" info \"%2\"")
                              .arg(QDir::currentPath())
                              .arg(backupPath);

    runCommandLine(commandLine);
}

//获取当前系统盘盘符
QString CSystemReloadThread::getSystemDrive()
{
    char str[MAX_PATH]{0};
    GetSystemDirectoryA(str, MAX_PATH);
    return QString::fromStdString(std::string(1, str[0]));
}


//磁盘格式化(需要在winPE执行)
//参数:盘符(c) 、 文件系统(ntfs)
bool CSystemReloadThread::FormatVolume(QString drive, QString format)
{
    QString program = "cmd.exe";
    QStringList arguments;
    arguments << "/c" << "format " + drive + ": /FS:" + format + " /Q /Y";


    QProcess p;
    p.start(program, arguments);
    if (p.waitForStarted())
    {
        p.waitForFinished();
        qDebug() << "成功";
        return true;
    }
    else
    {
        qDebug() << "失败";
        return false;
    }

    return 0;
}


//通过命令行执行外部程序
int CSystemReloadThread::runCMD(std::string cmd)
{
    HANDLE hPipeRead, hPipeWrite;
    SECURITY_ATTRIBUTES saAttr;
    STARTUPINFOA siStartInfo;
    DWORD dwRead;
    CHAR chBuf[4096];

    // 创建匿名管道
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;
    if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
    {
        return 1;
    }

    // 为子进程设置文件句柄
    SetHandleInformation(hPipeRead, HANDLE_FLAG_INHERIT, 0);

    // 初始化 STARTUPINFOA 结构体
    ZeroMemory(&siStartInfo, sizeof(STARTUPINFOA));
    siStartInfo.cb = sizeof(STARTUPINFOA);
    siStartInfo.hStdOutput = hPipeWrite;
    siStartInfo.hStdError = hPipeWrite;
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

    // 启动外部程序
    if (!CreateProcessA(NULL, (LPSTR)cmd.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo))
    {
        return 1;
    }

    // 关闭管道的写入端,这样在读取管道时可以知道进程已结束
    CloseHandle(hPipeWrite);

    // 循环读取管道中的输出信息
    while (ReadFile(hPipeRead, chBuf, sizeof(chBuf), &dwRead, NULL) && dwRead > 0)
    {
        // 在这里对 chBuf 中的输出信息进行处理
        // 例如输出到控制台
        m_DoingOutput = QString::fromStdString(ANSItoUTF8(chBuf));
        emit sigUpdateDoingText(m_DoingOutput);

//        QFile logfile(QDir::currentPath()+ "/SystemBackup/test.log");
//        logfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append);
//        QDataStream out(&logfile);
//        out<< m_DoingOutput <<"\r\n";
//        logfile.close();
    }

    // 等待子进程结束
    WaitForSingleObject(piProcInfo.hProcess, INFINITE);

    // 关闭句柄
    CloseHandle(piProcInfo.hProcess);
    CloseHandle(piProcInfo.hThread);
    CloseHandle(hPipeRead);

    return 0;
}


//关闭正在执行的外部程序
void CSystemReloadThread::killProcess()
{
    if(piProcInfo.hProcess){
        TerminateProcess(piProcInfo.hProcess,0);
        CloseHandle(piProcInfo.hProcess);
        CloseHandle(piProcInfo.hThread);
    }
}



//下载文件
bool CSystemReloadThread::downloadFile(QString md5, const QString& downloadUrl, const QString& downloadDirPath, const QString& filename)
{
    //请求链接
    QStringList list = downloadUrl.split("/");
    QString filename2 = list.at(list.size() - 1);

    if (filename.length() > 0)
        filename2 = "/" + filename;

    //存放文件夹
    QString downloadDir(downloadDirPath);
    QDir dir(downloadDir);
    if (!dir.exists()) {
        if (!dir.mkpath(downloadDir))
            qDebug() << "failed to create dir";
    }

    //打开目标文件,先open,便于后续分块写入
    QString downloadName(downloadDir + filename2);
    QFile downloadFile(downloadName);
    if (!downloadFile.open(QIODevice::ReadWrite)) {
        qDebug() << "open downloadFile error";
        return false;
    }

    //发送HTTP-Get请求
    QEventLoop* loop = new QEventLoop;
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QNetworkRequest request;
    request.setUrl(QUrl(downloadUrl));

    QNetworkReply* reply = manager->get(request);
    if (!reply) {
        qDebug() << "Failed to send request!";
        return false;
    }
    //信号与槽接收响应
    //分块获取文件信息,并写入文件中
    QObject::connect(reply, &QNetworkReply::readyRead, this, [&]() {
            downloadFile.write(reply->readAll());
        }, Qt::DirectConnection);

    //定时获取下载进度
    qint64 curtype = 0;
    qint64 oldtype = 0;
    qint64 totaltype = 0;
    int usetime = 0;
    QTimer* time = new QTimer(this);
    time->start(1000);
    QObject::connect(time, &QTimer::timeout, this, [&] {
            qint64 speed = curtype - oldtype;
            oldtype = curtype;
            usetime++;
        }, Qt::DirectConnection);

    //网络请求完成时
    QObject::connect(reply, &QNetworkReply::finished, this, [&]() {
            //处理HTTP响应
            if (reply->error() != QNetworkReply::NoError) {
                qDebug() << "Error:" << reply->errorString();
                qDebug() << "download failed!";
                downloadFile.close();
                m_DownloadFlag = false;
                dir.remove(downloadName);
            }
            else {
                downloadFile.write(reply->readAll());
                downloadFile.close();
                qDebug() << "download success";

                //检验文件
                QFile theFile(downloadName);
                if (!theFile.open(QIODevice::ReadOnly)) {
                    qDebug() << "open failed";
                }

                QCryptographicHash hash(QCryptographicHash::Md5);
                const qint64 blockSize = 1024 * 1024 * 100;  // 每次读取的块大小
                QByteArray buffer;
                while (!theFile.atEnd()) {
                    buffer = theFile.read(blockSize);
                    hash.addData(buffer);
                }
                QByteArray md5file = hash.result();

                theFile.close();
                if (md5file.toHex().constData() != md5.toLower()) {
                    m_DownloadFlag = false;
                    DeleteFileA(downloadName.toStdString().c_str());
                }
                else {

                }
            }

            manager->deleteLater();
            reply->deleteLater();
            time->deleteLater();
            loop->exit();
        }, Qt::DirectConnection);

    // 下载进度处理
    QObject::connect(reply, &QNetworkReply::downloadProgress, [&](qint64 bytesReceived, qint64 bytesTotal) {
        //qDebug() << "Downloaded" << bytesReceived << "out of" << bytesTotal << "bytes";
        curtype = bytesReceived;
        totaltype = bytesTotal;
    });

    loop->exec();
    return m_DownloadFlag;
}




//解压压缩包
//传参:压缩包路径、目标文件夹路径
void CSystemReloadThread::unZip(const char* zipName, const char* dirName) {
    int iErr = 0;
    struct zip* zipfile = NULL;
    struct zip_file* entries = NULL;
    struct zip_stat stat;
    zip_int64_t i64Num = 0;
    zip_int64_t i64Count = 0;
    int iRead = 0;
    int iLen = 0;
    char buf[1024];

    memset(&stat, 0, sizeof(stat));
    memset(buf, 0, sizeof(buf));

    zipfile = zip_open(zipName, ZIP_CHECKCONS, &iErr);
    if (!zipfile)
    {
        printf("zip open failed:%d\n", iErr);
        exit(EXIT_FAILURE);
    }

    //get how many entrrites in archive
    i64Num = zip_get_num_entries(zipfile, 0);
    for (i64Count = 0; i64Count < i64Num; i64Count++)
    {
        iLen = 0;
        if (zip_stat_index(zipfile, i64Count, 0, &stat) == 0)
        {
            printf("the file name is:%s\n", stat.name);
        }

        entries = zip_fopen_index(zipfile, i64Count, 0);
        if (!entries)
        {
            printf("fopen index failed\n");
            goto End;
        }

        //create the original file
        char filePath[MAX_PATH]{ 0 };
        strcpy(filePath, dirName);
        strcat(filePath, "/");
        strcat(filePath, stat.name);

        if (filePath[strlen(filePath) - 1] == '/') {
            QDir tempDir(filePath);
            if (!tempDir.exists()) {
                if (!tempDir.mkdir(filePath))
                    qDebug() << "failed to create dir";
            }
            zip_fclose(entries);
            continue;
        }

        FILE* fp = fopen(filePath, "wb+");
        if (!fp)
        {
            printf("create local file failed\n");
            zip_fclose(entries);
            goto End;
        }

        while (iLen < stat.size)
        {
            iRead = zip_fread(entries, buf, 1024);
            if (iRead < 0)
            {
                printf("read file failed\n");
                fclose(fp);
                zip_fclose(entries);
                goto End;
            }

            fwrite(buf, 1, iRead, fp);
            iLen += iRead;
        }

        zip_fclose(entries);
        fclose(fp);
    }

End:
    zip_close(zipfile);
}




//拷贝文件夹
//CopyDir("e:\\zyh","f:\\zyh");
bool CSystemReloadThread::CopyDir(const QString& fromDir, const QString& toDir)
{
    QDir sourceDir(fromDir);
    QDir targetDir(toDir);
    if (!targetDir.exists()) {    /**< 如果目标目录不存在,则进行创建 */
        if (!targetDir.mkpath(targetDir.absolutePath())) {
            qDebug() << targetDir.absolutePath();
            return false;
        }
    }

    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList) {
        if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;

        if (fileInfo.isDir()) {    /**< 当为目录时,递归的进行copy */
            if (!CopyDir(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName())))
                return false;
        }
        else {            /**< 当允许覆盖操作时,将旧文件进行删除操作 */
            if (targetDir.exists(fileInfo.fileName())) {
                targetDir.remove(fileInfo.fileName());
            }

            /// 进行文件copy
            if (!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) {
                return false;
            }
            else {
                qDebug() << "copy file" << fileInfo.filePath() << "success";
            }
        }
    }
    return true;
}


本次主要利用该工具做系统备份和还原,经研究发现,极客狗装机和老毛桃之类的第三方系统操作软件,调用的其实也是wimlib-imagex.exe,因此进行参考,但是发现利用其备份系统时,有时候遇到某些文件会导出备份出错,暂不清楚原因。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1284700.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Opencv打开图片

cv.imread() oepncv中使用cv.imread函数读取图片&#xff0c;并打开窗口显示&#xff0c;以下是示例代码 import cv2 as cv import numpy as np from matplotlib import pyplot as pltsrc cv.imread("demo.jpg")#blue, green red cv.namedWindow("input ima…

数据库系统概论期末经典大题讲解(用关系代数进行查询)

今天也是结束的最为密集的考试周&#xff0c;在分析过程中自己也有些许解题的感悟&#xff0c;在此分享出来&#xff0c;希望能帮到大家期末取得好成绩。 一.专门的关系运算 1.选择&#xff08;σ&#xff09; 选择操作符用于从关系中选择满足特定条件的元组 例如&#xff0c;…

前端——html拖拽原理

文章目录 ⭐前言⭐draggable属性&#x1f496; api&#x1f496; 单向拖动示例&#x1f496; 双向拖动示例 ⭐总结⭐结束 ⭐前言 大家好&#xff0c;我是yma16&#xff0c;本文分享关于 前端——html拖拽原理。 vue3系列相关文章&#xff1a; vue3 fastapi 实现选择目录所有文…

安装you-get(mac)

1、首先要有python环境 2、更新pip python -m pip install --upgrade pip 3、安装you-get pip install you-get;

面试多线程八股文十问十答第二期

面试多线程八股文十问十答第二期 作者&#xff1a;程序员小白条&#xff0c;个人博客 相信看了本文后&#xff0c;对你的面试是有一定帮助的&#xff01; ⭐点赞⭐收藏⭐不迷路&#xff01;⭐ 1.进程和线程的区别 概念不同&#xff1a;进程是操作系统中的一个独立执行单元&a…

redis 安装在liunx安装和常用文件配置

文章目录 安装配置文件设置测试启动服务连接服务 安装 1.官网下载压缩包: https://redis.io/download/ 2.将压缩包上传到Linux环境中 解压: tar -xvf redis-xxxxx 3.liunx 需要c的环境 yum -y install gcc-c4.进入redis文件夹 make && make install5.推荐不是必须…

【滤波第二期】中值滤波的原理和C代码

中值滤波是一种非线性数字滤波技术&#xff0c;主要应用于信号处理和图像处理领域&#xff0c;用于减小信号中的噪声和离群值。中值滤波的核心思想是通过计算一组数据点的中间值&#xff0c;以抑制脉冲噪声等离群值的影响&#xff0c;从而实现信号的平滑处理。 1&#xff0c;中…

spring cloud nacos整合gateway

文章目录 gateway快速入门创建gateway服务&#xff0c;引入依赖编写启动类编写基础配置和路由规则重启测试网关路由的流程图 断言工厂过滤器工厂路由过滤器的种类请求头过滤器默认过滤器总结 全局过滤器全局过滤器作用自定义全局过滤器过滤器执行顺序 跨域问题什么是跨域问题解…

浅谈Django之单元测试

一、什么是单元测试 单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。如果测试通过则说明我们这个函数或功能能够正常工作&#xff0c;如果失败要么测试用例不正确&#xff0c;要么函数有bug需要修复。 二、如何使用单元测试 from django.test imp…

使用UART和USART在STM32上进行双向通信

在本文中&#xff0c;我们将深入了解如何在STM32上使用UART&#xff08;通用异步收发传输器&#xff09;和USART&#xff08;通用同步异步收发传输器&#xff09;实现双向通信。UART和USART是常见的串口通信协议&#xff0c;通常用于与其他设备进行数据传输。我们将重点介绍如何…

抓取Chrome所有版本密码

谷歌浏览器存储密码的方式 在使用谷歌浏览器时,如果我们输入某个网站的账号密码,他会自动问我们是否要保存密码,以便下次登录的时候自动填写账号和密码 在设置中可以找到登录账户和密码 也可以直接看密码,不过需要凭证 这其实是windows的DPAPI机制 DPAPI Data Protection Ap…

ESP32 freeRTOS笔记 参数传递、任务优先级

一、四种参数传递方式 1.1 整数传递 使用 (void *) 任何类型传递参数&#xff0c;通过地址传递给任务。 #include <stdio.h> #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h"void myTask(void *pvP…

leetcode算法之栈

目录 1.删除字符串中的所有相邻重复项2.比较含退格的字符串3.基本计算器II4.字符串解码5.验证栈序列 1.删除字符串中的所有相邻重复项 删除字符串中的所有相邻重复项 class Solution { public:string removeDuplicates(string s) {string ret;//使用数组模拟栈操作for(auto …

element的el-date-picker时间控件,限制选择范围区间天数并且当前之后的日期不可选

element的el-date-picker时间控件&#xff0c;限制选择范围区间天数并且当前之后的日期不可选 HTML部分代码 <el-date-pickerv-model"dateRange"type"datetimerange"value-format"yyyy-MM-dd HH:mm:ss"range-separator"至"start-p…

Python小案例:打印10以内的素数

解析 1、利用循环控制范围&#xff08;1,100&#xff09; 2、通过循环判断素数 3、利用标记法进行打印素数 代码 #求1——100之间的素数 for i in range(2,101):is_primeNum Truefor j in range(2,i):if i%j 0:# print(f"{i}不是素数")is_primeNum Falseif is_…

3D Web可视化平台助力Aras开发PLM系统:提供数据访问、可视化和发布功能

HOOPS中文网慧都科技是HOOPS全套产品中国地区指定授权经销商&#xff0c;提供3D软件开发工具HOOPS售卖、试用、中文试用指导服务、中文技术支持。http://techsoft3d.evget.com/ Aras是一个面向数字化工业应用的开放性平台&#xff0c;帮助世界领先的复杂互联产品制造商转变其产…

文字处理工具Word mac软件特点

Microsoft Word mac是一款文字处理软件。它是 Microsoft office 套件的一部分&#xff0c;已广泛用于创建、编辑和格式化文本文档。 Word mac软件特点 改进的协作工具&#xff1a;使用 Microsoft Word 2021&#xff0c;多个用户可以同时处理一个文档&#xff0c;从而更轻松地与…

(华为)网络工程师教程笔记(网工教程)网工入门——3、静态路由路由表的配置

参考文章&#xff1a;【全236集】网络工程师从基础入门到进阶必学教程&#xff01;通俗易懂&#xff0c;2023最新版&#xff0c;学完即可就业&#xff01;网工入门_华为认证_HCIA_HCIP_数据通信_网工学习路线 文章目录 13. 网工入门10-静态路由&#xff08;路由表的配置&#x…

STM32通用定时器

本文实践&#xff1a;实现通过TIM14_CH1输出PWM&#xff0c;外部显示为呼吸灯。 通用定时器简介 拥有TIM2~TIM5、TIM9~TIM14 一共10个定时器&#xff0c;具有4路独立通道&#xff0c;可用于输入捕获、输出比 较&#xff0c;同时包含了基本定时去的所有功能。 通用定时器的结…

Educational Codeforces Round 159 (Rated for Div. 2)(B 二分贪心 Cgcd D二分+前缀和 E字典树)

A - Binary Imbalance 有只要在01之间插入就能制造无限个0&#xff0c;没有0就统计0 1个数即可 #include<bits/stdc.h> using namespace std; const int N 110010,mod998244353; #define int long long typedef long long LL; typedef pair<int, int> PII; const…