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

📄 downcfg.~pas

📁 完全自我开发的downcfg控件
💻 ~PAS
字号:
//作者:庄斗
//zhuangd@21cn.com
//下载单元,创建类TDownCFG配置实例,
//server服务器http全名,无则在配置文件中区
//inifile:ini配置文件
//section:读取的部分
//【×××】《--section
//  server 《--服务器,传入为空时取此值
//  from  《--服务器文件全路径 及文件
//  to 《--本地存放文件相对(当前执行目录)路径及文件
//  method  《--下载后处理方法    1:直接存盘
//           2:运行,直到运行结束
//           3:运行,直接返回
//           9:为信息显示
//  ver   《--版本号
//  desc 《--文件描述
//使用:
//给ProcessBar赋值
//downfile下载文件
//writecfg更新CFG文件
//exefile执行下载的文件
//downcfg直接从服务器下载升级文件信息http://server/update/server.ini
//compareCFG比较2个文件,返回需要下载的文件个数,注意文件名的路径
{modify:
  2004.3.14  增加downcfg,writecfg函数
             修改downfile的try部分
  2004.3.20  修改下载存盘路径方式,直接参盘到toto中,可以灵活设置toto值
             增加zompareCFG函数
}
unit downcfg;

interface
uses
   forms,windows,classes,SysUtils,inifiles, IdBaseComponent, IdComponent, IdTCPConnection,
   IdTCPClient, IdHTTP,IdGlobal, ComCtrls,dialogs,IdAntiFreezeBase,
  IdAntiFreeze;
type
TDownCFG=class
    inifile:string;
    ini:Tinifile;  //配置文件
    Server:string; //http服务器
    From:string;   //下载URL
    toto:string;   //存盘URL
    ver:string;    //版本号
    method:string; //处理方法
    section:string;//部分
    desc:string;//本描述
    error:string;//错误信息
    param:string;//运行参数
    fileCnt:integer;//下载文件个数
    ProgressBar: TProgressBar;//进度条
    procedure http1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
        const AWorkCountMax: Integer);
    procedure http1Work(Sender: TObject; AWorkMode: TWorkMode;
        const AWorkCount: Integer);
    procedure http1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
  private
    FHttpOnBeginWork:TWorkBeginEvent ;
    FHttpOnWork:TWorkEvent ;
    FHttpOnWorkEnd:TWorkEndEvent ;
    iDiv:integer;
  public
    property OnBeginWork:TWorkBeginEvent read FHttpOnBeginWork write FHttpOnBeginWork;
    property OnWork:TWorkEvent read FHttpOnWork write FHttpOnWork;
    property OnWorkEnd:TWorkEndEvent read FHttpOnWorkEnd write FHttpOnWorkEnd;
    function downfile:boolean;
    procedure writeCFG(source:TDownCFG;desc:TDownCFG);
    function  Exefile:boolean;
    function  downcfg:boolean;
    function  CompareCFG(server:string;client:string):integer;
    constructor Create(server1:string;inifile:string;section:string); //override;
  end;
var
 anFree: TIdAntiFreeze;
implementation

uses DownMess;

procedure TDownCFG.http1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
begin
  ProgressBar.Position:=0;
  ProgressBar.Max:=aworkcountmax;
  iDiv:=1;
  if aworkcountmax>3000000 then
    iDiv:=60
  else if aworkcountmax>1000000 then
    iDiv:=20
  else if aworkcountmax>500000 then
   iDiv:=2;
end;

//下载条控制
procedure TDownCFG.http1Work(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
  ProgressBar.Position:=ProgressBar.Position+aworkCount div iDiv;
end;

procedure TDownCFG.http1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
 ProgressBar.Position:=0;
end;

//运行文件直到结束,返回,进行下一步
function WinExecAndWait32(FileName:String; Visibility:integer):integer;
var
  zAppName:array[0..512] of char;
  zCurDir:array[0..255] of char;
  WorkDir:String;
  i:cardinal;
  StartupInfo:TStartupInfo;
  ProcessInfo:TProcessInformation;
begin
  StrPCopy(zAppName,FileName);
  GetDir(0,WorkDir);
  StrPCopy(zCurDir,WorkDir);
  FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if not CreateProcess(nil,zAppName, { 执行文件}
     nil, { 指向安全属性 }
     nil, { 执行thread属性 }
     false, { handle标识 }
     CREATE_NEW_CONSOLE or { 标识 }
     NORMAL_PRIORITY_CLASS,
     nil, { 新的环境块 }
     nil, { 当前目录 }
     StartupInfo, { 指向STARTUPINFO }
     ProcessInfo)
           then Result := -1 { pointer to PROCESS_INF }
  else
    begin
       WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
       GetExitCodeProcess(ProcessInfo.hProcess,i);
       result:=i;
    end;
end;

function TDownCFG.downcfg:boolean;
begin
   self.from:='http://'+server+'/update/server.ini';
   self.toto:='server.ini';
   if self.downfile then
    result:=true
   else
    result:=false;
end;

//比较配置文件,返回需要下载的文件数
function TDownCFG.compareCFG(server:String;client:string):integer;
var
 i,cnt,k:integer;
 iniS,iniC:tinifile;
 vS,vC:string;
begin
 k:=0;
 iniS:=tinifile.Create(server);
 iniC:=tinifile.Create(server);
 cnt:= iniS.ReadInteger('server','filecnt',0);
 for i:=1 to cnt do
  begin
    vS:=iniS.ReadString('file'+inttostr(i),'ver','');
    vC:=iniC.ReadString('file'+inttostr(i),'ver','');
    if vS>vC then
      inc(k);
  end;
 iniS.Free;
 iniC.Free;
 result:=k;
end ;

//更新配置文件
procedure TDownCFG.writeCFG(source:TDownCFG;desc:TDownCFG);
begin
  ini:=tinifile.Create(extractfilepath(paramstr(0))+desc.inifile);
  ini.WriteString('server','host',source.Server);
  ini.WriteString(section,'From',source.From);
  ini.WriteString(section,'To',source.toto);
  ini.WriteString(section,'Ver',source.ver);
  ini.WriteString(section,'Method',source.method);
  ini.WriteString(section,'Desc',source.desc);
  ini.Free;
end;

//下载文件后的处理
{9:显示信息
 1:下载保存
 2:下载运行,结束后返回
 3:下载运行,直接返回
}
function TDownCFG.ExeFile:boolean;
var
  id:integer;
begin
    result:=true;
    case strtoint(method) of
      9://显示消息
      begin
       mess:=TMess.Create(nil);
       mess.left:= (screen.Width-mess.Width) div 2;
       mess.top:=(screen.Height-mess.Height) div 2;
       mess.RE1.Lines.LoadFromFile(extractfilepath(paramstr(0))+toto);
       mess.ShowModal;
       mess.Free;
      end;
    1:;//存盘文件
    2://执行文件,直到文件运行结束
      begin
        //winexec(pchar(extractfilepath(paramstr(0))+toto+' '+param),sw_shownormal);
        WinExecAndWait32(extractfilepath(paramstr(0))+toto+' '+param,1);
        //showmessage('end');
      end;
    3://执行文件,立即返回
      begin
        winexec(pchar(extractfilepath(paramstr(0))+toto+' '+param),sw_shownormal);
      end;
   end;
end;

//下载文件,from-->toto
function TDownCFG.downfile:boolean;
var
    MyStream:TMemoryStream;
    HTTP1: TIdHTTP;
begin
  if not assigned(ProgressBar) then
  begin
    error:='配置文件初始化失败processBar';
    result:=false;
    exit;
  end;
  if toto='' then
   begin
     error:='目标文件空';
     result:=false;
     exit;
   end;

  try
    MyStream:=TMemoryStream.Create;
  except
    result:=false;
    error:='创建Stream失败';
    exit;
  end;
  try
    http1:=TIdHTTP.Create(nil);
  except
    result:=false;
    error:='创建http失败';
    MyStream.Free;
    exit;
  end;
  //初始化http控件
    http1. MaxLineAction := maException ;
    http1.AllowCookies:= True;
    http1.ReadTimeout:=0;
    http1.ProxyParams.BasicAuthentication := False;
    http1.ProxyParams.ProxyPort := 0;
    http1.Request.ContentLength := -1;
    http1.Request.ContentRangeEnd := 0;
    http1.Request.ContentRangeStart := 0;
    http1.Request.ContentType := 'text/html' ;
    http1.Request.Accept := 'text/html, */*' ;
    http1.Request.BasicAuthentication := False;
    http1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
    http1.HTTPOptions := [hoForceEncodeParams];

    http1.OnWork:=OnWork;;
    http1.OnWorkBegin:=OnBeginWork;;
    http1.OnWorkEnd:=OnWorkEnd;;
    try
      http1.Get(from,Mystream);
      result:=true;
    except
     on e:exception do
      begin
        result:=false;
        error:=e.Message;
        mystream.Free;
        http1.Free;
        exit;
      end;
    end;
    chdir(extractfilepath(paramstr(0)));
    try
      MyStream.SaveToFile(toto);
    except
      on e:exception do
       begin
        result:=false;
        error:=e.Message;
       end;
    end;
    mystream.Free;
    http1.Free;
end;

//类实例创建,初始化一些值
constructor TDownCFG.Create(server1:string;inifile:string;section:string);
begin
  inherited Create;
  ini:=tinifile.Create(extractfilepath(paramstr(0))+inifile);
  if server1<>'' then
    server:=server1
  else
    server:=ini.ReadString('server','host','127.0.0.1');
  fileCnt:=ini.ReadInteger('server','filecnt',0);
  param:=ini.ReadString(section,'param','');
  from:=ini.ReadString(section,'From','');
  Toto:=ini.ReadString(section,'To','');
  ver:=ini.ReadString(section,'ver','');
  method:=ini.ReadString(section,'Method','');
  desc:=ini.ReadString(section,'Desc','');
  self.inifile:=inifile;
  self.section:=section;
  FHttpOnBeginWork:= http1WorkBegin;
  FHttpOnWork:= http1Work;
  FHttpOnWorkEnd:= http1WorkEnd;
  ini.Free;
   anFree:= TIdAntiFreeze.Create(nil);
   anfree.OnlyWhenIdle:=false;
end;


end.

⌨️ 快捷键说明

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