高手进来,给个算法吧!
请哪位高手好心帮我翻译以下代码的算法:#include <vcl.h>
#pragma hdrstop
#include "Extract.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TExtract::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TExtract::TExtract(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
#include "Unit1.h"
//---------------------------------------------------------------------------
void __fastcall TExtract::Execute()
{
String BaseDir = ExtractFileDir(FileName)+"\\out_hxf";
MkDir(BaseDir);
int fp = FileOpen(FileName,fmOpenRead);
if( fp < 0 ){
MessageBox(Application->Handle,"文件打开的失败","出错",MB_OK);
return ;
}
int FileLen = FileSeek(fp,0,2);
FileSeek(fp,0x40,0);
while( FileSeek(fp,0,1) < FileLen ){
ExtractFile(fp,BaseDir);
}
strMsg = "解包完成";
this->Synchronize(AddLog);
FileClose(fp);
}
void __fastcall TExtract::AddLog(void)
{
//TODO: Add your source code here
Form1->Memo1->Lines->Insert(0,strMsg);
}
void TExtract::ExtractFile( int fp , String Base){
DWORD length,rCount;
char s[400],buf[1024000];
int n = 0 ;
FileRead(fp,&length,4);
if( length > 400 || length <= 0 ){
strMsg = "文件名长度不对";
this->Synchronize(AddLog);
return;
}
FileRead(fp,s,length);
s[length]=0;
Base = Base + "\\";
for( char * t = s ; *t != 0 ; t++){
if( *t == '\\'){
*t=0;
MkDir(Base+String(s));
*t='\\';
}
}
String FileName = Base+String(s);
strMsg = FileName ;
//this->Synchronize(AddLog);
if( FileExists(FileName)){
DeleteFile(FileName);
}
int fout = FileCreate(FileName,0777);
if( fout <= 0 ) return ;
FileRead(fp,buf,1);
FileRead(fp,&length,4);
for( rCount = length ; rCount>0;rCount-=n){
n = FileRead(fp,buf,rCount>1024000?1024000:rCount);
FileWrite(fout,buf,n);
}
FileClose(fout);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include
#include "Unit1.h"
#include "stdio.h"
#include
using namespace std;
#pragma hdrstop
#include "Package.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TPackage::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TPackage::TPackage(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
bool compare(String first, String second)
{
unsigned int i=0;
while ( (i<FIRST.LENGTH())
{
if (tolower(first.c_str())<TOLOWER(SECOND.C_STR()))
else if (tolower(first.c_str())>tolower(second.c_str())) return false;
++i;
}
if (first.Length()<SECOND.LENGTH())
else return false;
}
void TPackage::PackageFile(int fp , String BaseDir){
char sBuf[100],*buf;
String FullName;
int dwSize,n;
int iAttributes = faAnyFile | faDirectory;
TSearchRec sr;
int hxfPos , fin ;
hxfPos = BaseDir.Pos("hxf");
hxfPos+=4;
if(FindFirst(BaseDir+"*.*", iAttributes, sr) == 0){
list mylist;
list mydir;
do{
if( sr.Name.c_str()[0] == '.' )continue;
if( (sr.Attr & faDirectory) > 0){
mylist.push_back("D"+sr.Name);
continue;
}
mylist.push_back("d"+sr.Name);
strMsg = "开始为" + sr.Name + "打包"; this->Synchronize(AddLog);
}while (FindNext(sr) == 0);
mylist.sort(compare);
list::iterator it;
if( mylist.size() > 0 )
for (it=mylist.begin(); it!=mylist.end(); ++it){
char c = (*it).c_str()[0];
String Name = (*it).SubString(2,(*it).Length()-1);
if( c == 'D'){
PackageFile(fp,BaseDir+Name+"\\");
continue;
}
FullName = BaseDir+Name;
FullName = FullName.SubString(hxfPos,FullName.Length()-hxfPos+1);
fin = FileOpen(BaseDir+Name, fmOpenRead);
if( fin<0){
strMsg = "文件错误"; this->Synchronize(AddLog);
continue;
}
int FileLen = FileSeek(fin,0,2);
dwSize = FullName.Length();FileWrite(fp,&dwSize,4);
FileWrite(fp,FullName.c_str(),dwSize);
FileWrite(fp," ",1);
FileWrite(fp,&FileLen,4);
FileSeek(fin,0,0);
buf = new char[102400];
for( int rCount = FileLen , n = 0 ; rCount>0;rCount-=n){
n = FileRead(fin,buf,rCount>102400?102400:rCount);
FileWrite(fp,buf,n);
}
free(buf);
FileClose(fin);
}
}
/*
if(FindFirst(BaseDir+"*.*", iAttributes, sr) == 0)
do
{
if( sr.Name.c_str()[0] == '.' )continue;
if( (sr.Attr & faDirectory) > 0){
PackageFile(fp,BaseDir+sr.Name+"\\");
continue;
}
//if( sr.Name == "Thumbs.db" )continue;
FullName = BaseDir+sr.Name;
FullName = FullName.SubString(hxfPos,FullName.Length()-hxfPos+1);
fin = FileOpen(BaseDir+sr.Name, fmOpenRead);
if( fin<0){
strMsg =
可能有点不完整!!