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

📄 main.cpp

📁 zip算法的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}
	// Determine which conversion is to be done.
	if ( UpperCase( ExtractFileExt( ZipBuilder1->ZipFilename ) ) == ".EXE" ) {
		// Convert .EXE to .ZIP
		if ( (ConvertErr = ZipBuilder1->ConvertZIP()) == 0 )
			ShowMessage( "Filename is now: " + ZipBuilder1->ZipFilename );
		else
			ShowMessage( "Error " + IntToStr( ConvertErr ) + " occured in making .ZIP file" );
	} else {
		// Convert .ZIP to .EXE
		/* NOTE: If you put the ZIPSFX.BIN file into the WINDOWS
			or WINDOWS SYSTEM dir, then you don't need to set the
			SFXPath property below: */
		// ZipBuilder1->SFXPath = "c:\\windows\\system\\zipsfx.bin";
		MakeSFX->ShowModal();
		if ( DoIt == false ) return;
		if ( (ConvertErr = ZipBuilder1->ConvertSFX()) == 0 )
			ShowMessage( "Filename is now: " + ZipBuilder1->ZipFilename );
		else
			ShowMessage( "Error " + IntToStr( ConvertErr ) + " occured in making .EXE file" );
	}
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::VerboseCBClick( TObject *Sender ) {
	ZipBuilder1->Verbose = VerboseCB->Checked;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::TraceCBClick( TObject *Sender ) {
	ZipBuilder1->Trace = TraceCB->Checked;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::UnattendedCBClick(TObject *Sender) {
	ZipBuilder1->Unattended = UnattendedCB->Checked;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::Showlasterror1Click( TObject *Sender ) {
	if ( ZipBuilder1->ErrCode )
		ShowMessage( IntToStr( ZipBuilder1->ErrCode ) + " " + ZipBuilder1->Message );
	else
		ShowMessage( "No last error present" );
}

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

//---------------------------------------------------------------------------
void __fastcall TMainForm::Zipcomment1Click( TObject *Sender ) {
	if ( ZipBuilder1->ZipComment != "" ) {
		MsgForm->RichEdit1->Clear();
		MsgForm->RichEdit1->Lines->Add( ZipBuilder1->ZipComment );
		MsgForm->Show();
	} else
		ShowMessage( "No Zip comment in this zip file" );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::DLLversioninfo1Click( TObject *Sender ) {
	ShowMessage( "UnZip Dll version: " + IntToStr( ZipBuilder1->UnzVers ) + "\n" +
					 "  Zip Dll version: " + IntToStr( ZipBuilder1->ZipVers ) );
}

//***********************ZipBuilder Event handling***************************
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1Message( TObject *Sender, int ErrCode, String Message ) {
	MsgForm->RichEdit1->Lines->Append( Message );
	::PostMessage( MsgForm->RichEdit1->Handle, EM_SCROLLCARET, 0L, 0L );
	Application->ProcessMessages();
	if ( ErrCode > 0 && !ZipBuilder1->Unattended ) ShowMessage( "Error Msg: " + Message );
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1DirUpdate( TObject *Sender ) {
	FillGrid();
	FilesLabel->Caption = IntToStr( ZipBuilder1->Count );
	if ( UpperCase( ExtractFileExt( ZipBuilder1->ZipFilename ) ) == ".EXE" )
		ConvertBut->Caption = "Convert to ZIP";
	else
		ConvertBut->Caption = "Convert to EXE";
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1Progress( TObject *Sender, ProgressType ProgrType, String Filename, long FileSize ) {
	static long	TotalSize1, TotalProgress1, TotalSize2, TotalProgress2;
	int			Step;

	switch ( ProgrType ) {
		case TotalSize2Process:
			// ZipBuilder1Message( this, 0, "in OnProgress type TotalBytes, size= " + IntToStr( FileSize ) );
			MsgForm->StatusBar1->Panels->Items[0]->Text = "Total size: " + IntToStr( (int)FileSize / 1024 ) + " Kb";
			MsgForm->ProgressBar2->Position = 1;
			TotalSize2     = FileSize;
			TotalProgress2 = 0;
			break;

		case TotalFiles2Process:
			// ZipBuilder1Message( this, 0, "in OnProgress type TotalFiles, files= " + IntToStr( FileSize ) );
			MsgForm->StatusBar1->Panels->Items[1]->Text = IntToStr( (int)FileSize ) + " files";
			break;

		case NewFile:
			// ZipBuilder1Message( this, 0, "in OnProgress type NewFile, size= " + IntToStr( FileSize ) );
			MsgForm->FileBeingZipped->Caption = Filename;
			MsgForm->ProgressBar1->Position = 1;		// Current position of bar.
			TotalSize1		 = FileSize;
			TotalProgress1	 = 0;
			break;

		case ProgressUpdate:
			// ZipBuilder1Message( this, 0, "in OnProgress type Update, size= " + IntToStr( FileSize ) );
			// FileSize gives now the bytes processed since the last call.
			TotalProgress1 += FileSize;
			TotalProgress2 += FileSize;
			if ( TotalSize1 ) {
				Step = (int)( (unsigned __int64)TotalProgress1 * 10000ui64 / (unsigned __int64)TotalSize1 );
				MsgForm->ProgressBar1->Position = (PGBCONV)(1 + Step);
			} else MsgForm->ProgressBar1->Position = 10001;
			if ( TotalSize2 ) {
				Step = (int)( (unsigned __int64)TotalProgress2 * 10000ui64 / (unsigned __int64)TotalSize2 );
				MsgForm->ProgressBar2->Position = (PGBCONV)(1 + Step);
			}
			break;

		case EndOfBatch:
			// Reset the progress bar and filename.
			// ZipBuilder1Message( this, 0, "in OnProgress type EndOfBatch" );
			MsgForm->FileBeingZipped->Caption = "";
			MsgForm->ProgressBar1->Position = 1;
			MsgForm->StatusBar1->Panels->Items[0]->Text = "";
			MsgForm->StatusBar1->Panels->Items[1]->Text = "";
			MsgForm->ProgressBar2->Position = 1;
	}
}

//***********************User defined functions *****************************
//---------------------------------------------------------------------------
String TMainForm::ShowLTime( long s, long f ) {
	String smin, ssec;	// ,smil

	int st = f - s;
   //int mil = st % 1000;
   //mil %= 60;
	int sec = st / 1000;
	int min = sec / 60;
	sec %= 60;
	//if (mil > 9) smil = inttostr(mil); else smil = "0" + IntToStr(mil);
	ssec = ( sec > 9 ) ? IntToStr( sec ) : "0" + IntToStr( sec );
	smin = ( min > 9 ) ? IntToStr( min ) : "0" + IntToStr( min );
	return smin + ":" + ssec;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::SetZipFName( String aCaption, bool AssignName ) {
	// Assigning the filename will cause the table of contents to be read.
	// and possibly reset it to an empty string (If error found).
	if ( AssignName ) ZipBuilder1->ZipFilename = aCaption;

	ZipFName->Caption = (ZipBuilder1->ZipFilename == "") ? AnsiString( "<none>" ) :
		MinimizeName( ZipBuilder1->ZipFilename, ZipFName->Canvas, ZipFName->Width );

	if ( ZipFName->Canvas->TextWidth( ZipBuilder1->ZipFilename ) > ZipFName->Width ) {
		ZipFName->Hint     = ZipBuilder1->ZipFilename;
		ZipFName->ShowHint = true;
	} else ZipFName->ShowHint = false;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::SetZipTotals( void ) {
	StringGrid1->Cells[0][ZipBuilder1->Count + 1] = "Total";
	StringGrid1->Cells[1][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)TotComp );
	StringGrid1->Cells[2][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)TotUncomp );
	unsigned __int64 cs = TotComp, us = TotUncomp;
	if ( us )
		StringGrid1->Cells[4][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)(100 - cs * 100 / us - ((cs * 100 % us) >= us / 2) ) ) + " % ";
	else
		StringGrid1->Cells[4][ZipBuilder1->Count + 1] = "0 % ";
	StringGrid1->Cells[5][ZipBuilder1->Count + 1] = "";
}

//---------------------------------------------------------------------------
bool __fastcall TMainForm::AskDirDialog( const HWND FormHandle, String &DirPath ) {
	BROWSEINFO  bi;
	LPSTR		   lpBuffer;
	ITEMIDLIST *pidl;          // PIDL selected by user
	LPMALLOC    g_pMalloc;
	bool			Result = false;

	// Get the shell's allocator.
	if ( (SHGetMalloc( &g_pMalloc ) )== E_FAIL ) return false;

	// Allocate a buffer to receive browse information.
	if ( (lpBuffer = (LPSTR)g_pMalloc->Alloc( MAX_PATH)) != 0 ) {
		bi.hwndOwner		= FormHandle;
		bi.pidlRoot			= NULL;
		bi.pszDisplayName	= lpBuffer;
		bi.lpszTitle		= "";
		bi.ulFlags			= 0;
		bi.lpfn				= NULL;
		bi.lParam			= 0;

		// if pidl = 0 then cancel is used.
		if ( (pidl = SHBrowseForFolder( &bi )) != 0 ) {
			// if 0 then pidl not part of namespace
			if ( SHGetPathFromIDList( pidl, lpBuffer ) ) {
				DirPath = lpBuffer;
				if ( DirPath[DirPath.Length()] != '\\' ) DirPath += '\\';
				Result  = true;
			}
			// Free the PIDL returned by SHBrowseForFolder.
			g_pMalloc->Free( pidl );
		}
		// Free the string buffer used for the name
		g_pMalloc->Free( lpBuffer );
	}
	// Release the shell's allocator.
	g_pMalloc->Release();
	return Result;
}

//---------------------------------------------------------------------------
/* Folder types are a.o.
 *	CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU, CSIDL_SENDTO,
 * CSIDL_PROGRAMS, CSIDL_STARTUP etc.
 */
long __fastcall TMainForm::GetSpecialFolder( int aFolder, String &Location ) {
	long		   FolderErr = 0;   // No error.
	char		  *RealPath;
	ITEMIDLIST *pidl;
	LPMALLOC    g_pMalloc;

	// Get the shell's allocator.
	if ( ::SHGetMalloc( &g_pMalloc ) == E_FAIL ) return E_FAIL;

	// Allocate a buffer to receive the path information.
	if ( (RealPath = (char *)g_pMalloc->Alloc( MAX_PATH )) != NULL ) {
		HRESULT hRes = ::SHGetSpecialFolderLocation( Handle, aFolder, &pidl );
		if ( hRes == NOERROR ) {
			bool Success = ::SHGetPathFromIDList( pidl, RealPath );
			if ( Success ) {
				Location = RealPath;
				Location += "\\";
			} else FolderErr = E_UNEXPECTED;
			// Free the PIDL allocated by SHGetSpecialFolderLocation.
			g_pMalloc->Free( pidl );
		} else FolderErr = hRes;
		// Free the string buffer used for the name
		g_pMalloc->Free( RealPath );
	} else FolderErr = E_OUTOFMEMORY;
	// Release the shell's allocator.
	g_pMalloc->Release();
	return FolderErr;
}

//**************************Grid functions **********************************
//---------------------------------------------------------------------------
void __fastcall TMainForm::FillGrid( void ) {
	// Remove everything from grid except col titles.
	StringGrid1->RowCount = 2;
	StringGrid1->ClearFrom( 1 );
	if ( !ZipBuilder1->Count ) return;

	StringGrid1->RowCount = ZipBuilder1->Count + 2;
	TotUncomp = TotComp   = 0;
	for ( int i = 1; i <= ZipBuilder1->Count; i++ ) {
		ZipDirEntry *zde = (ZipDirEntry *)ZipBuilder1->ZipContents->Items[i - 1];
		StringGrid1->Cells[0][i] = ExtractFileName( zde->FileName );
		StringGrid1->Cells[1][i] = IntToStr( (int)zde->CompressedSize );
		StringGrid1->Cells[2][i] = IntToStr( (int)zde->UncompressedSize );
		StringGrid1->Cells[3][i] = FormatDateTime( "ddddd  t", FileDateToDateTime( zde->DateTime ) );
		unsigned __int64 cs = zde->CompressedSize, us = zde->UncompressedSize;
		if ( us )
			StringGrid1->Cells[4][i] = IntToStr( (unsigned int)(100 - cs * 100 / us - ((cs * 100 % us) >= us / 2) ) ) + "% ";
		else
			StringGrid1->Cells[4][i] = "0% ";
		StringGrid1->Cells[5][i] = ExtractFilePath( zde->FileName );
		TotUncomp += us;
		TotComp   += cs;
	}
	TSortOptions so;
	so.SortDirection		= sdAscending;
	so.SortStyle			= ssAutomatic;
	so.SortCaseSensitive	= false;
	StringGrid1->SortByColumn( StringGrid1->SortColumn, so );
	StringGrid1->Row = 1;
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::StringGrid1EndSort( TObject *Sender, int Col ) {
	SetZipTotals();
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::StringGrid1GetCellFormat( TObject *Sender, int Col, int Row, TGridDrawState State, TFormatOptions &FormatOptions ) {
	if ( Row && Col && Col != 5 ) FormatOptions.AlignmentHorz = taRightJustify;
}

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

⌨️ 快捷键说明

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