⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 该程序实现从临时目录解压文件到当前目录的功能
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "main.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString strUpdateFile = ExtractFilePath( Application->ExeName ) + "\\temp\\123.rar" ;
    //AnsiString strUpdateFile = ExtractFilePath( Application->ExeName ) + "\\temp\\Msg.txt" ;

	pUnRar = new CUnRar(strUpdateFile.c_str());
       __int64 nTotalSize = pUnRar->GetUnpackSize();
    FILESIZEINFO *pFileListInfo = pUnRar->GetFileList();
    while (pFileListInfo)
    {
        Application->ProcessMessages();
    	int nResult = pUnRar->UnpackFile(pFileListInfo->szPath, ".\\");
        //ShowMessage(pFileListInfo->szPath);


        if (nResult == UNRAR_SUCCESS)
        {

            pFileListInfo = pFileListInfo->pNext;
            continue;
        }

        if (nResult == UNRAR_ERROR_FILEINUSE)
        {
            int i = 0;
            while ( i < 50 )          //给5秒钟的时间等待某些文件的完全释放
            {
                Application->ProcessMessages();
                nResult = pUnRar->UnpackFile(pFileListInfo->szPath, ".\\");

                if ( nResult == UNRAR_SUCCESS )
                {

                    pFileListInfo = pFileListInfo->pNext;
                    break;
                }
                Sleep( 600 );
                i++;
            }

            if ( nResult == UNRAR_SUCCESS )
            {
            	continue;
            }

            return;
        }
        return;
    }

    delete pUnRar;
    //CopyUpFile(ExtractFilePath(Application->ExeName)+"temp\\",ExtractFilePath(Application->ExeName));
   //DeleteUpFile(ExtractFilePath(Application->ExeName)+"temp\\");

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{

    //int nResult = pUnRar->UnpackFile(pFileListInfo->szPath, ".\\");
}
//---------------------------------------------------------------------------
void GetFileList( AnsiString PathName, TStringList *pFileList )
{
    TSearchRec sr;
    AnsiString FullPath;
    if(FindFirst( PathName+"*.*", faAnyFile, sr) == 0 )
    {
        do
        {
            Application->ProcessMessages();
            if( sr.Name!="." && sr.Name!=".." )
            {
                if( FileExists( PathName+sr.Name ) )
                {
                    pFileList->Add(PathName+sr.Name);
                }
                if( DirectoryExists( PathName+sr.Name ) )
                {
                    FullPath=PathName+sr.Name;
                    if( FullPath.SubString( FullPath.Length(), 1 )!="\\" )
                    {
                        FullPath=FullPath+"\\";
                        GetFileList( FullPath, pFileList );
                        Application->ProcessMessages();
                    }
                }
            }
        }while (FindNext(sr) == 0);
        FindClose(sr);
    }
}
//---------------------------------------------------------------------------------
void GetPathFile( AnsiString PathName, TStringList *FileList )
{
    TSearchRec sr;
    AnsiString FullPath;
    if(FindFirst( PathName+"*.*", faAnyFile, sr) == 0 )
    {
        do
        {
            Application->ProcessMessages();
            if( sr.Name!="." && sr.Name!=".." )
            {
                if( FileExists( PathName+sr.Name ) )
                {
                    FileList->Add(PathName+sr.Name);
                }
                if( DirectoryExists( PathName+sr.Name ) )
                {
                    FullPath=PathName+sr.Name;
                    if( FullPath.SubString( FullPath.Length(), 1 )!="\\" )
                    {
                        FullPath=FullPath+"\\";
                        GetFileList( FullPath, FileList );
                        Application->ProcessMessages();
                    }
                }

            }

        }while (FindNext(sr) == 0);

        FindClose(sr);
    }
}
//------------------------------------------------------------------------------
void __fastcall TForm1::CopyUpFile( AnsiString SourcePath, AnsiString DestPath )
{
    TStringList *FileList=new TStringList;
    AnsiString AppPath=ExtractFilePath( Application->ExeName );
    FileList->Clear();
    GetPathFile( SourcePath, FileList );
    AnsiString SourceFile, DestFile, SourceF;
    int iPos;
    if( FileList->Count>0 )
    {
        for( int i=0; i<FileList->Count; i++ )
        {
            SourceFile=FileList->Strings[i];
            iPos=SourcePath.Length();

            SourceF=SourceFile.SubString( iPos+1, SourceFile.Length() );
            DestFile=DestPath+SourceF;
            if( !DirectoryExists( ExtractFileDir( DestFile ) ) )
            {
                ForceDirectories( ExtractFileDir( DestFile ) );
            }
            Application->ProcessMessages();
            CopyFile( SourceFile.c_str(), DestFile.c_str(), false );
            Application->ProcessMessages();
        }
    }
    FileList->Clear();
    delete FileList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DeleteUpFile( AnsiString Path )
{
    TStringList *FileList=new TStringList;
    AnsiString AppPath=ExtractFilePath( Application->ExeName );
    FileList->Clear();
    GetPathFile( Path, FileList );
    if( FileList->Count>0 )
    {
        for( int i=0; i<FileList->Count; i++ )
        {
            if( FileExists( FileList->Strings[i] ) )
                DeleteFile( FileList->Strings[i] );
            Application->ProcessMessages();
        }
    }
    delete FileList;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -