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

📄 mainunit.cpp

📁 这份是一份压缩解压缩的商业产品的源码JZIP。压缩算法比较不错
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    }
    else if (ExtractForm->AllRadioButton->Checked)
    {
        ZipBuilder->FSpecArgs->Add("*.*");
    }
    else // Files...
    {
        if (ExtractForm->FilesEdit->Text == "*")
            ZipBuilder->FSpecArgs->Add("*.*");
        else
            ZipBuilder->FSpecArgs->Add(ExtractForm->FilesEdit->Text);
    }

    // Make dir if it doesn't already exists
    ZipBuilder->ExtrBaseDir = ExtractForm->ExtractToComboBox->Text;
    ForceDirectories(ZipBuilder->ExtrBaseDir);
    if (!DirectoryExists(ZipBuilder->ExtrBaseDir))
    {
        Application->MessageBox("Error creating extraction directory!\nExtraction failed.", "JZip Error", MB_ICONERROR | MB_OK);
        return;
    }

    // Extract
    ZipBuilder->Extract();

    // Update the system
    Update();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ExtractToolButtonClick(TObject *Sender)
{
    // Perform Extract
    Extract1Click(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ExtListViewMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    // Save Mouse coord (used in ExtListViewDblClick)
    iMouseX = X;
    iMouseY = Y;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ExtListViewDblClick(TObject *Sender)
{
    // Get the item that was double clicked
    TListItem* Item = ExtListView->GetItemAt(iMouseX, iMouseY);
    if (Item)
    {

        // Extract the file (unless if it's a setup.exe or install.exe file, then we extract the entire zip file)
        if (Item->Caption.LowerCase() == "setup.exe" || Item->Caption.LowerCase() == "install.exe")
        {
            // This is the same as Install
            Install1Click(NULL);

        }
        else
        {
            // Extract the file to the temp dir
            // Make temp dir
            AnsiString TempDir = CreateTempDir();
            // Add the temp dir to the CleanUpList
            CleanUpList->Add(TempDir);

            // Set options
            ZipBuilder->ExtrOptions.Clear();
            ZipBuilder->ExtrOptions << ExtrOverWrite;

            // Set FSpecArgs
            ZipBuilder->FSpecArgs->Clear();
            if (Item->SubItems->Strings[4] == "")
                ZipBuilder->FSpecArgs->Add(Item->Caption);
            else
                ZipBuilder->FSpecArgs->Add(Item->SubItems->Strings[4]+"\\"+Item->Caption);

            // Make dir if it doesn't already exists
            ZipBuilder->ExtrBaseDir = TempDir;
            CreateDir(ZipBuilder->ExtrBaseDir);
            // Save file to open before extraction and update
            AnsiString tmp = TempDir+"\\"+Item->Caption;

            ZipBuilder->Extract();
            Update();

            int rc = (int)   ShellExecute(Handle,
                                 "open",
                                 tmp.c_str(),
                                 NULL,NULL,SW_SHOWDEFAULT);
            if (rc <= 32)
            {
                if (rc == SE_ERR_NOASSOC)
                    Application->MessageBox("Sorry, there's no default viewer for this file type!","View Error", MB_OK | MB_ICONERROR);
                else
                    Application->MessageBox("An error ocurred while spawning the viewer application!","View Error", MB_OK | MB_ICONERROR);
            }
        }
    }
}
//---------------------------------------------------------------------------
// Create a temporary directory in the windows temp dir.
AnsiString __fastcall TMainForm::CreateTempDir()
{
    // Local vars
    AnsiString asTmp;
    char szTmp[MAX_PATH];
    int tmp;

    // Create a unique dir name (combined of numbers)
    // As an improvement letters could later be used, but it's not that crucial
    while(1)
    {
        asTmp = "";
        for(int iLoop=0;iLoop < 8; iLoop++)
        {
            tmp = random(58);
            while(tmp <48 || tmp > 57)
                tmp = random(58);
            sprintf(szTmp, "%c",tmp);
            asTmp += szTmp;
        }

        asTmp = ZipBuilder->TempDir+asTmp;
        if (!SetCurrentDir(asTmp))
        {
            CreateDir(asTmp);
            break;
        }
    } // end while

    // Return the dir name
    return asTmp;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Install1Click(TObject *Sender)
{
    // Install
    // Set options
    ZipBuilder->ExtrOptions.Clear();
    ZipBuilder->ExtrOptions << ExtrDirNames;
    ZipBuilder->ExtrOptions << ExtrOverWrite;

    // Set FSpecArgs
    ZipBuilder->FSpecArgs->Clear();
    ZipBuilder->FSpecArgs->Add("*.*");

    // Make temp dir
    AnsiString TempDir = CreateTempDir();
    // Add the temp dir to the CleanUpList
    CleanUpList->Add(TempDir);

    // Make dir if it doesn't already exists
    ZipBuilder->ExtrBaseDir = TempDir;
    CreateDir(ZipBuilder->ExtrBaseDir);

    // Perform the extraction
    ZipBuilder->Extract();

    // Update the system
    Update();

    // Change dir to the extraction dir
    SetCurrentDir(TempDir);

    // Execute the installation program
    ShellExecute(Handle,
                 "open",
                 InstallProgram.c_str(),
                 NULL,NULL,SW_SHOWDEFAULT);

    // Show the install form
    InstallForm->ShowModal();

    // Delete files
    DeleteDir(TempDir);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::InstallToolButtonClick(TObject *Sender)
{
    // Perform install
    Install1Click(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::View1Click(TObject *Sender)
{
    // View a file
    // Get the item that was double clicked
    TListItem* Item = ExtListView->Selected;
    if (Item)
    {

        // Extract the file (unless if it's a setup.exe or install.exe file, then we extract the entire zip file)
        if (Item->Caption.LowerCase() == "setup.exe" || Item->Caption.LowerCase() == "install.exe")
        {
            // This is the same as Install
            Install1Click(NULL);

        }
        else
        {
            // Extract the file to the temp dir
            // Make temp dir
            AnsiString TempDir = CreateTempDir();
            // Add the temp dir to the CleanUpList
            CleanUpList->Add(TempDir);

            // Set options
            ZipBuilder->ExtrOptions.Clear();
            ZipBuilder->ExtrOptions << ExtrOverWrite;

            // Set FSpecArgs
            ZipBuilder->FSpecArgs->Clear();
            if (Item->SubItems->Strings[4] == "")
                ZipBuilder->FSpecArgs->Add(Item->Caption);
            else
                ZipBuilder->FSpecArgs->Add(Item->SubItems->Strings[4]+"\\"+Item->Caption);

            // Make dir if it doesn't already exists
            ZipBuilder->ExtrBaseDir = TempDir;
            CreateDir(ZipBuilder->ExtrBaseDir);
            // Save file to open before extraction and update
            AnsiString tmp = TempDir+"\\"+Item->Caption;

            // Set workaround flag
            bExtractSelected = true;

            // Perform extraction
            ZipBuilder->Extract();

            // Update system
            Update();


            int rc = (int)   ShellExecute(Handle,
                                 "open",
                                 tmp.c_str(),
                                 NULL,NULL,SW_SHOWDEFAULT);
            if (rc <= 32)
            {
                if (rc == SE_ERR_NOASSOC)
                    Application->MessageBox("Sorry, there's no default viewer for this file type!","View Error", MB_OK | MB_ICONERROR);
                else
                    Application->MessageBox("An error ocurred while spawning the viewer application!","View Error", MB_OK | MB_ICONERROR);
            }
        }
    }

}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ViewToolButtonClick(TObject *Sender)
{
    // Perform view
    View1Click(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CloseArchive1Click(TObject *Sender)
{
    // Close archive (reinit)
    ZipBuilder->ZipFileName = "";
    ZipBuilder->Password = "";
    Caption = "JZip";
    Update();
}
//---------------------------------------------------------------------------
// Delete a dir (and all files in it)
void __fastcall TMainForm::DeleteDir(AnsiString Dir)
{
    // Local static vars
    static char szTmp[MAX_PATH];

    AnsiString asTmpDir = GetCurrentDir();
    if (!SetCurrentDir(Dir))
        return;     // Error the directory didn't exist
    else
        SetCurrentDir(asTmpDir);

    StatusBar->Panels->Items[1]->GaugeAttrs->Style = gsIndeterminate2;
    StatusBar->Panels->Items[1]->PanelType = sptGauge;
    StatusBar->Panels->Items[1]->Enabled = true;


    // Set filename to be deleted (memset's used because SHFileOp expects double \0\0 as termination of string
    memset(szTmp, 0, MAX_PATH*sizeof(char));
    strcpy(szTmp, Dir.c_str());

    // Set up the struct
    SHFILEOPSTRUCT op;
    memset(&op,0,sizeof(SHFILEOPSTRUCT));
    op.hwnd  = Handle;  // Handle of main form or the application
    op.wFunc = FO_DELETE;
    op.pFrom = szTmp;
    op.pTo   = NULL;
    op.fFlags= FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI ;

    // Perform the deletion
    SHFileOperation(&op);

    // Set dir back to startup dir
    SetCurrentDir(StartupDirectory);

    // Update statusbar
    StatusBar->Panels->Items[1]->GaugeAttrs->Style = gsPercent;
    StatusBar->Panels->Items[1]->PanelType = sptNormal;
    StatusBar->Panels->Items[1]->Enabled = true;
    StatusBar->Panels->Items[1]->Alignment = taLeftJustify;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Comment1Click(TObject *Sender)
{
    // Init the Comment form
    CommentForm->Memo->Lines->BeginUpdate();
    CommentForm->Memo->Text = ZipBuilder->ZipComment;
    CommentForm->Memo->Lines->EndUpdate();
    CommentForm->Comment->Clear();

    // Show
    CommentForm->ShowModal();

    // Save comment?
    if (CommentForm->bSave)
    {
        ZipBuilder->ZipComment = CommentForm->Comment->Text;
    }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Test1Click(TObject *Sender)
{
    // Set options
    ZipBuilder->ExtrOptions.Clear();
    ZipBuilder->ExtrOptions << ExtrTest;

    // Set FSpecArgs
    ZipBuilder->FSpecArgs->Clear();

    // Do the testing
    bTesting = true;
    iTestErrors = 0;
    ZipBuilder->Extract();
    bTesting = false;

    // Update the system
    Update();

    // Show test result
    // Improvement: Perform a scroolup to the top. Look at: EM_LINEINDEX
    TestForm->ShowModal();
}
//---------------------------------------------------------------------------
// Zip error message
void __fastcall TMainForm::ZipBuilderMessage(TObject *Sender, int ErrCode,
      AnsiString Message)
{
    if (bTesting)
    {
        if (ErrCode == 0)
            TestForm->Memo->Lines->Add(Message+"      OK");
        else
        {
            TestForm->Memo->Lines->Add(Message+"      Failed");
            iTestErrors++;
        }
    }
    else if (ErrCode == 590089)  // Operation cancelled
    {
        Application->MessageBox("Operation was cancelled!", "JZip", MB_OK | MB_ICONWARNING);
        Update();
        ExtListViewChange(NULL, NULL, NULL);

⌨️ 快捷键说明

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