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

📄 main.cpp

📁 zip算法的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <vcl\vcl.h>
#pragma hdrstop

#include "Main.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

#pragma link "Grids"
#pragma link "SortGrid"
#pragma link "ZIPBuilder"

TMainForm *MainForm;

String ExtractDir;
bool ExpandDirs, OverWr, AllFiles, Canceled;

//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate( TObject *Sender ) {
	/* Make sure "goColMoving" is false in object inspector. This lets the
      TSortGrid use Mouse Clicks on the col headers. */
	StringGrid1->RowCount     =   2;  // First and last row are fixed.
	StringGrid1->Cells[0][0]  = "File Name";
	StringGrid1->Cells[1][0]  = "Compr. Size";
	StringGrid1->Cells[2][0]  = "Uncompr. Size";
	StringGrid1->Cells[3][0]  = "Date/Time";
	StringGrid1->Cells[4][0]  = "Ratio";
	StringGrid1->Cells[5][0]  = "Path";

	ZipBuilder1->Load_Zip_Dll();
	ZipBuilder1->Load_Unz_Dll();
	/* If we had args on the cmd line, then try to open the first one
		as a zip/exe file.  This is most useful in case user has an association
		to ".zip" that causes this program to run when user double clicks on a zip
		file in Explorer. */
	if ( ParamCount > 0 ) ZipBuilder1->ZipFilename = ParamStr( 1 );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::FormResize( TObject *Sender ) {
	ZipFName->Width = max( 0, Width - 291 );
	SetZipFName( ZipBuilder1->ZipFilename, false );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::CloseButClick( TObject *Sender ) {
	Close();
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::FormDestroy( TObject *Sender ) {
	ZipBuilder1->Unload_Zip_Dll();
	ZipBuilder1->Unload_Unz_Dll();
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipOpenButClick( TObject *Sender ) {
	static String FirstDir = "";

	if ( FirstDir == "" ) GetSpecialFolder( CSIDL_DESKTOPDIRECTORY, FirstDir );
	OpenDialog1->InitialDir = FirstDir;
	OpenDialog1->Title      = "Open Existing ZIP File";
	OpenDialog1->Filter     = "ZIP Files (*.ZIP, *.EXE)|*.zip;*.exe";
	OpenDialog1->FileName   = "";
	OpenDialog1->Options << ofHideReadOnly << ofShareAware << ofPathMustExist << ofFileMustExist;
	if ( OpenDialog1->Execute() ) {
		FirstDir = ExtractFilePath( OpenDialog1->FileName );
		/* Set the caption after assigning the filename. This
		   way, the filename will be null if the open failed. */
		SetZipFName( OpenDialog1->FileName, true );
	}
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::NewZipButClick( TObject *Sender ) {
	static String FirstDir = "";

	if ( FirstDir == "" ) GetSpecialFolder( CSIDL_DESKTOPDIRECTORY, FirstDir );
	OpenDialog1->InitialDir = FirstDir;
	OpenDialog1->FileName   = "";
	OpenDialog1->Filter     = "ZIP Files (*.ZIP)|*.zip";
	OpenDialog1->DefaultExt = "Zip";
	OpenDialog1->Title      = "Create New ZIP File";
	OpenDialog1->Options << ofHideReadOnly << ofShareAware;
	OpenDialog1->Options >> ofPathMustExist >> ofFileMustExist;
	if ( OpenDialog1->Execute() ) {
		FirstDir = ExtractFilePath( OpenDialog1->FileName );
		if ( FileExists( OpenDialog1->FileName ) ) {
			bool Ans = MessageDlg( "Overwrite Existing File: " + OpenDialog1->FileName + "?",
                          mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0 ) == mrYes;
			if ( Ans )
				DeleteFile( OpenDialog1->FileName );
			else return;  // Don't use the new name.
		}
		SetZipFName( OpenDialog1->FileName, true );
	}
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::DeleteZipButClick( TObject *Sender ) {
	if ( FileExists( ZipBuilder1->ZipFilename ) ) {
		bool Ans = MessageDlg( "Are you sure you want to delete: " + ZipBuilder1->ZipFilename
            + "?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0 ) == mrYes;
		if ( Ans ) {
			DeleteFile( ZipBuilder1->ZipFilename );
			SetZipFName( "", true );
		} else return;  // Don't use the new name.
	} else
		ShowMessage( "Zip file not found: " + ZipBuilder1->ZipFilename );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ExtractButClick( TObject *Sender ) {
   if ( !FileExists( ZipBuilder1->ZipFilename ) ) {
		ShowMessage( "Error: file not found: " + ZipBuilder1->ZipFilename );
		return;
	}
	Extract->ShowModal();
	if ( ( ExtractDir == "" ) || Canceled ) return;

	if ( ZipBuilder1->Count < 1 ) {
		ShowMessage( "Error - no files to extract" );
		return;
	}
	ZipBuilder1->FSpecArgs->Clear();
	// Get fspecs of selected files, unless user wants all files extracted.
	if ( !AllFiles ) {
		for ( int i = 1; i <= StringGrid1->SelectedCount; i++ ) {
			int SelRow = StringGrid1->SelectedItems[ i ];
			if ( SelRow > 0 && (SelRow != StringGrid1->RowCount - 1) )
				ZipBuilder1->FSpecArgs->Add( StringGrid1->Cells[5][SelRow] + StringGrid1->Cells[0][SelRow] );
		}
		if ( ZipBuilder1->FSpecArgs->Count < 1 ) {
			ShowMessage( "Error - no files selected" );
			return;
		}
	}
	MsgForm->RichEdit1->Clear();
	MsgForm->Show();
	// Put this message into the message form.
	ZipBuilder1Message( this, 0, "Beginning Extract from " + ZipBuilder1->ZipFilename );

	ZipBuilder1->ExtrBaseDir = ExtractDir;
	ZipBuilder1->ExtrOptions.Clear();
	if ( ExpandDirs )
		ZipBuilder1->ExtrOptions << ExtrDirNames;
	if ( OverWr )
		ZipBuilder1->ExtrOptions << ExtrOverWrite;
	s = ::GetTickCount();
	try {
		ZipBuilder1->Extract();
	} catch ( ... ) {
		ShowMessage( "Error in Extract; Fatal DLL Exception in Main" );
		return;
	}
	f = ::GetTickCount();
	TimeLabel->Caption = ShowLTime( s, f );
	String IsOne = (ZipBuilder1->SuccessCnt == 1) ? " was" : "s were";
	ShowMessage( IntToStr( ZipBuilder1->SuccessCnt ) + " file" + IsOne + " extracted" );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::AddButClick( TObject *Sender ) {
	if ( ZipBuilder1->ZipFilename == "" ) {
		ShowMessage( "Error - open a zip file first" );
		return;
	}
	AddFile->Left   = Left;
	AddFile->Top    = Top;
	AddFile->Width  = Width;
	AddFile->Height = Height;
	Canceled = false;
	AddFile->ShowModal();  // Let user pick filenames to add.
	if ( Canceled ) return;

	if ( !AddFile->SelectedList->Items->Count ) {
		ShowMessage( "No files selected" );
		return;
	}
	MsgForm->RichEdit1->Clear();
	MsgForm->Show();
	// Put this message into the message form.
	ZipBuilder1Message( this, 0, "Beginning Add to " + ZipBuilder1->ZipFilename );

	/* We want any DLL error messages to show over the top
		of the message form. */
	ZipBuilder1->AddOptions.Clear();
	switch ( AddFile->ZipAction ) {	// Default is plain ADD.
		case 2:
			ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddUpdate;   // Update
			break;
		case 3:
			ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddFreshen;  // Freshen
			break;
		case 4:
			ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddMove;     // Move
	}
	if ( AddFile->RecurseCB->Checked )					// We want recursion.
		ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddRecurseDirs;
	if ( AddFile->DirNameCB->Checked )					// We want dirnames.
		ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddDirNames;
	if ( AddFile->DiskSpanCB->Checked )					// We disk spanning.
		ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddDiskSpan;
	if ( AddFile->EncryptCB->Checked ) {				// We want a password.
		ZipBuilder1->AddOptions = ZipBuilder1->AddOptions << AddEncrypt;
		// ZipBuilder1->GetAddPassword();
		// if ( ZipBuilder1->Password == "" )
			// The 2 password's entered by user didn't match.
			/* We'll give him one more try; if he still messes it
				up, the DLL itself will prompt him one final time. */
      //   ZipBuilder1->GetAddPassword();
	}
	ZipBuilder1->FSpecArgs->Clear();
	ZipBuilder1->FSpecArgs->Assign( AddFile->SelectedList->Items ); // Specify filenames.
	AddFile->SelectedList->Clear();
	s = ::GetTickCount();
	try {
		ZipBuilder1->Add();
	} catch ( ... ) {
		ShowMessage( "Error in Add; Fatal DLL Exception in Main" );
		return;
	}
	f = ::GetTickCount();
	TimeLabel->Caption = ShowLTime( s, f );
	String IsOne = (ZipBuilder1->SuccessCnt == 1) ? " was" : "s were";
	ShowMessage( IntToStr( ZipBuilder1->SuccessCnt ) + " file" + IsOne + " added" );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::DeleteButClick( TObject *Sender ) {
	if ( ZipBuilder1->Count < 1 ) {
		ShowMessage( "Error - no files to delete" );
		return;
	}
	bool Ans = MessageDlg( "Delete selected files from: " +
			ZipBuilder1->ZipFilename + "?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0 ) == mrYes;
	if ( !Ans ) return;

	ZipBuilder1->FSpecArgs->Clear();
	for ( int i = 1; i <= StringGrid1->SelectedCount; i++ ) {
		int SelRow = StringGrid1->SelectedItems[ i ];
		if ( SelRow > 0 && (SelRow != StringGrid1->RowCount - 1) )
			ZipBuilder1->FSpecArgs->Add( StringGrid1->Cells[5][SelRow] + StringGrid1->Cells[0][SelRow] );
	}
	if ( ZipBuilder1->FSpecArgs->Count < 1 ) {
		ShowMessage( "Error - no files selected" );
		return;
	}

	MsgForm->RichEdit1->Clear();
	MsgForm->Show();
	// Put this message into the message form.
	ZipBuilder1Message( this, 0, "Beginning delete from " + ZipBuilder1->ZipFilename );

	s = ::GetTickCount();
	try {
		ZipBuilder1->Delete();
	} catch ( ... ) {
		ShowMessage( "Fatal error trying to delete" );
		return;
	}
	f = ::GetTickCount();
	TimeLabel->Caption = ShowLTime( s, f );
	String IsOne = (ZipBuilder1->SuccessCnt == 1) ? " was" : "s were";
	ShowMessage( IntToStr( ZipBuilder1->SuccessCnt ) + " file" + IsOne + " deleted" );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::TestButClick( TObject *Sender ) {
	if ( ZipBuilder1->Count < 1 ) {
		ShowMessage( "Error - nothing to Test" );
		return;
	}
	if ( ZipBuilder1->ZipFilename == "" ) return;
	MsgForm->RichEdit1->Clear();
	MsgForm->Show();
	ZipBuilder1Message( this, 0, "Beginning test of " + ZipBuilder1->ZipFilename );

	ZipBuilder1->FSpecArgs->Clear();
	ZipBuilder1->ExtrOptions << ExtrTest;
	ZipBuilder1->FSpecArgs->Add( "*.*" ); // Test all the files in the .zip.
	// IMPORTANT: In this release, you must test all files.
	s = ::GetTickCount();
	ZipBuilder1->Extract();  // This will really do a test.

	f = ::GetTickCount();
	TimeLabel->Caption = ShowLTime( s, f );
 	if ( ZipBuilder1->Count + ZipBuilder1->DirOnlyCount == ZipBuilder1->SuccessCnt )
		ShowMessage( "All " + IntToStr( ZipBuilder1->Count + ZipBuilder1->DirOnlyCount ) + " files tested OK" );
	else
		ShowMessage( "ERROR: " + IntToStr( ZipBuilder1->Count + ZipBuilder1->DirOnlyCount - ZipBuilder1->SuccessCnt ) +
                     " files tested BAD, or skipped!" );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::MsgButClick( TObject *Sender ) {
	MsgForm->Show();
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ConvertButClick( TObject *Sender ) {
	int ConvertErr;

	if ( !ZipBuilder1->Count ) {
		ShowMessage( "Error: no files in archive" );
		return;

⌨️ 快捷键说明

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