delphi结合WinRAR生成自解压文件  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi结合WinRAR生成自解压文件


最近写的一个程序需要将新编译的文件打包成自解压文件供用户去安装,在程序中通过WinRAR命令结合WinRAR自解压注释命令实现该功能。

WinRAR命令
WinRAR自解压注释命令 

程序界面:



文件目录 为要压缩的目录,默认安装目录为执行生成的自解压文件时默认的安装目录,备注为执行自解压文件时显示的注释。
自解压文件执行界面:
[SinglePic not found]

程序的实现:

首先定义一个公共的自解压注释命令模板 SFX_COMMENT_TEMPLATE.txt

Title=文件安装  

SavePath  

Path=@Path  

Text  

{  

      

          

            

恒生资产管理系统

  

            

  

                注意事项  

                

  • 在没有确定执行该安装时,请慎重执行
  •   

                    
  • 请事先做好相应的备份
  •   

                

      

                

      

                    备注  

                    
      

                    @Comment  

                

      

              

       

          



    @Path 执行自解压文件默认安装目录

    @Comment为备注信息
    程序根据用户选择依据该模板文件生成具体的自解压注释名利文件 comment.txt,并在WinRAR生成解压文件时关联该注释文件。
    生成自解压文件代码:procedure TBuildInstallationForm.btnBuildClick(Sender: TObject);  

    var 

      cmd,CommentTemplate,comment,FileStr,ExeFile: string;  

      FileList,TempList : TStringList;  

      i : Integer;  

    begin 

      if Trim(edtDir.Text) = '' then 

      begin 

        ShowMessage('文件目录不能为空');  

        Exit;  

      end;  

      //打开文件保存对话框  

      with dlgSave do 

      begin 

        if Execute then 

        begin 

          ExeFile := FileName;//要保存的自解压文件  

        end;  

      end;  

       

      CommentTemplate := ExtractFilePath(Application.ExeName) + 'SFX_COMMENT_TEMPLATE.txt';  

      comment := ExtractFilePath(Application.ExeName) + 'comment.txt';  

      FileList := TStringList.Create;  

      FindDirFile(edtDir.Text,FileList); //只获取该目录下的文件 和文件夹2.1.9  

      TempList := TStringList.Create;  

      TempList.LoadFromFile(CommentTemplate); //加载模板文件  

      //注释模板文件较小,这种处理方式不会对性能造成明显影响  

      with TempList do 

      begin 

        Text := StringReplace(Text,'@Path',edtDefaultDir.Text,[rfReplaceAll]);//替换文件路径  

        Text := StringReplace(Text,'@Comment',editComment.Text,[rfReplaceAll]);//替换注释  

      end;  

      TempList.SaveToFile(comment);//保存到注释文件  

      FileStr := '';//文件列表  

      for i := 0 to FileList.Count - 1 do 

      begin 

        FileStr := FileStr + ' "' + fileList.Strings[i] + '"';  

      end;  

       

      //Config.WinRAR WinRAR.exe 路径  

      cmd := Format('"%s" a  -z"%s" -ep1 -sfx "%s" %s',[Config.WinRAR,comment,ExeFile,FileStr]);  

      WinExec(PChar(cmd),SW_SHOWNORMAL);  

      ShowMessage('生成成功!');  

    end; 

    FindDirFile:

    procedure FindDirFile(ASourceDir:string;var fileList : TStringList);  

      var 

      sr : TSearchRec;  

      i : Integer;  

      found : Integer;  

    begin 

      if ASourceDir[Length(ASourceDir)]<>'\' then ASourceDir := ASourceDir + '\';  

      //找出所有的dof文件  

      try 

      found := FindFirst(ASourceDir + '*.*',faAnyFile,sr);  

        while found = 0 do 

        begin 

           if (sr.Name <> '.') and (sr.Name <> '..')then //(sr.Attr =  faDirectory) then  

           begin 

             fileList.Add(ASourceDir + sr.Name );  

           end;  

           found := FindNext(sr);  

        end;  

      finally 

        Findclose(sr);  

      end;  

    end; 

    本文只是对WinRAR命令和自解压注释命令的简单应用,读者可以依据二者创建出功能更加丰富的自解压工具。也欢迎大家分享其他的实现方法。 


    推荐分享
    图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
     

    Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
    晋ICP备14006235号-8 晋公网安备 14108102000087号

    执行时间: 0.058317899703979 seconds