杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2537|回复: 0

Inno Setup 创建带外部文件的安装程序

[复制链接]
发表于 2017-1-9 17:02:51 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用 INNO 制作安装程序,通常是把软件的所有文件打包(压缩)到一个安装程序文件 setup.exe。其实 INNO 也可以制作带外部文件的安装程序,也就是制作安装程序时并不把文件打包,只是在运行安装程序时才把这些外部文件(比如从光盘中)复制到软件的安装目录或者系统中的其它位置。写 [Files] 段的脚本时,只要使用标签 external 就可以做到这一点,比如:
[Files]
Source: "{src}\*.doc"; DestDir: "{app}"; Flags: external

用这种方式制作的安装包有个缺点就是文件数量多,而且文件没有被压缩,因此占用的空间大。

下面介绍另外一种制作带外部文件的安装包的方法。假设你的软件除了必需安装的文件外,还可以包括某些常用的插件(组件1)以及某些高级插件(组件2)。你希望在你的安装程序中只是打包那些必需安装的文件,而组件1和组件2则分别单独打包(压缩)为外部文件。这样做的好处是,用户可以根据自己的需要只下载安装程序以及某些组件,而你的安装程序则可以根据这些外部文件存在的情况给用户相应的选择。

这些外部文件是用 ZIP 格式压缩过的,为了不让人一看就知道是 ZIP 文件,所以将它们命名为 data1.dat、data2.dat 等等。

示例中使用的是一个简单的 ZIP 文件解压插件。当然你也可以使用其它比如支持带密码保护的 ZIP 文件的插件。另外你也可以在脚本中增加对插件外部文件进行校验的功能。
  1. ; Inno Setup 脚本
  2. ; 该示例脚本显示如何创建的带外部文件的安装程序。运行安装程序时用户可以根据外部文件存在的情况选择安装相应的组件。
  3. ; 外部文件保存在安装程序所的目录中的子目录 data 中,外部文件是 ZIP 压缩文件,命名为 data1.dat、data2.dat 等等
  4. ; 分别相应于组件 1、组件 2 等等。
  5. ;
  6. ;

  7. [Setup]
  8. AppName=组件安装示例程序
  9. AppVerName=组件安装示例程序 1.0
  10. DefaultDirName={pf}\_组件安装示例程序
  11. DefaultGroupName=组件安装示例程序

  12. [Messages]
  13. ComponentsDiskSpaceMBLabel=当前的选择至少需要 [mb] MB 磁盘空间(不包括解压后组件的大小)。

  14. [Files]
  15. ; 安装临时文件:解压外部文件所需的插件 unzipw32.dll
  16. Source: "unzipw32.dll"; DestDir: "{tmp}"; Flags: dontcopy
  17. ; 安装运行软件所必需的文件
  18. Source: "MyProg.exe"; DestDir: "{app}"; components: installmust
  19. Source: "MyProg.hlp"; DestDir: "{app}"; components: installmust

  20. [Components]
  21. Name: "installmust"; Description: "主程序(必需)"; Types: compact full custom; Flags: fixed
  22. Name: "installdata1"; Description: "组件 1 (常用插件)"; Types: full custom
  23. Name: "installdata2"; Description: "组件 2 (高级插件)"; Types: full custom

  24. [UninstallDelete]
  25. ; 卸载时删除外部解压的文件
  26. Type: filesandordirs; Name: "{app}\data1"
  27. Type: filesandordirs; Name: "{app}\data2"

  28. [Code]
  29. var dummysize, size1, size2: longint;

  30. // 从 unzipw32.dll 插件中要调用的函数
  31. function FileUnzipEx(SourceZipFile, TargetDirectory, FileSpecs: pChar ): integer;
  32. external 'FileUnzipEx@files:unzipw32.dll stdcall delayload';

  33. Function UnzipSize(SourceZipFile:pChar; Var Compressed:Longint):longint;
  34. external 'UnzipSize@files:unzipw32.dll stdcall delayload';

  35. // 点击组件列表后刷新显示的内容
  36. procedure ComponentsListOnClick(Sender: TObject);
  37. begin
  38. if WizardForm.ComponentsList.CHECKED[1] then
  39.   WizardForm.ComponentsList.ITEMSUBITEM[1]:= '解压后 ' + intTOstr(size1 div 1024) + ' KB';
  40. if not WizardForm.ComponentsList.ITEMENABLED[1] then
  41.   WizardForm.ComponentsList.ITEMSUBITEM[1]:= '无安装数据';
  42. if WizardForm.ComponentsList.CHECKED[2] then
  43.   WizardForm.ComponentsList.ITEMSUBITEM[2]:= '解压后 ' + intTOstr(size2 div 1024) + ' KB';
  44. if not WizardForm.ComponentsList.ITEMENABLED[2] then
  45.   WizardForm.ComponentsList.ITEMSUBITEM[2]:= '无安装数据';
  46. end;

  47. // 初始化安装向导
  48. procedure InitializeWizard();
  49. begin
  50. // 释放临时文件
  51. ExtractTemporaryFile(ExpandConstant('unzipw32.dll'));
  52. WizardForm.TYPESCOMBO.Visible:= false;
  53. WizardForm.ComponentsList.Onclick:= @ComponentsListOnClick;
  54. // 根据外部文件存在的情况确定哪些组件可用
  55. if not FileExists(ExpandConstant('{src}\data\data1.dat')) then
  56.   begin
  57.   WizardForm.ComponentsList.ITEMENABLED[1]:= false;
  58.   WizardForm.ComponentsList.CHECKED[1]:= false;
  59.   WizardForm.ComponentsList.ITEMSUBITEM[1]:= '无安装数据';
  60.   end
  61. else
  62.   begin
  63.   WizardForm.ComponentsList.ITEMENABLED[1]:= true;
  64.   WizardForm.ComponentsList.CHECKED[1]:= true;
  65.   size1 := UnzipSize(PChar(ExpandConstant('{src}\data\data1.dat')), dummysize);
  66.   WizardForm.ComponentsList.ITEMSUBITEM[1]:= '解压后 ' + intTOstr(size1 div 1024) + ' KB';
  67.   end;
  68. if not FileExists(ExpandConstant('{src}\data\data2.dat')) then
  69.   begin
  70.   WizardForm.ComponentsList.ITEMENABLED[2]:= false;
  71.   WizardForm.ComponentsList.CHECKED[2]:= false;
  72.   WizardForm.ComponentsList.ITEMSUBITEM[2]:= '无安装数据';
  73.   end
  74. else
  75.   begin
  76.   WizardForm.ComponentsList.ITEMENABLED[2]:= true;
  77.   WizardForm.ComponentsList.CHECKED[2]:= true;
  78.   size2 := UnzipSize(PChar(ExpandConstant('{src}\data\data2.dat')), dummysize);
  79.   WizardForm.ComponentsList.ITEMSUBITEM[2]:= '解压后 ' + intTOstr(size2 div 1024) + ' KB';
  80.   end;
  81. end;

  82. // 根据组件选择的情况从外部文件中解压组件到指定的位置,比如子目录 {app}\data1 和 {app}\data2
  83. procedure CurStepChanged(CurStep: TSetupStep);
  84. var n: integer;
  85. begin
  86. if CurStep=ssInstall then
  87.   begin
  88.   if IsComponentSelected('installdata1') then
  89.     n:= FileUnzipEx(PChar(ExpandConstant('{src}\data\data1.dat')), PChar(ExpandConstant('{app}\data1')), '*.*');
  90.   if IsComponentSelected('installdata2') then
  91.     n:= FileUnzipEx(PChar(ExpandConstant('{src}\data\data2.dat')), PChar(ExpandConstant('{app}\data2')), '*.*');
  92.   end;
  93. end;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-19 17:01 , Processed in 0.122487 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表