admin 发表于 2017-3-6 11:17:58

如何创建带外部文件的安装程序

; Inno Setup 脚本
; 该示例脚本显示如何创建的带外部文件的安装程序。运行安装程序时用户可以根据外部文件存在的情况选择安装相应的组件。
; 外部文件保存在安装程序所的目录中的子目录 data 中,外部文件是 ZIP 压缩文件,命名为 data1.dat、data2.dat 等等
; 分别相应于组件 1、组件 2 等等。
;
; 汉化新世纪 gnatix 于 2006.09.14


AppName=组件安装示例程序
AppVerName=组件安装示例程序 1.0
DefaultDirName={pf}\_组件安装示例程序
DefaultGroupName=组件安装示例程序


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


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


Name: "installmust"; Description: "主程序(必需)"; Types: compact full custom; Flags: fixed
Name: "installdata1"; Description: "组件 1 (常用插件)"; Types: full custom
Name: "installdata2"; Description: "组件 2 (高级插件)"; Types: full custom


; 卸载时删除外部解压的文件
Type: filesandordirs; Name: "{app}\data1"
Type: filesandordirs; Name: "{app}\data2"


var dummysize, size1, size2: longint;

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

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

// 点击组件列表后刷新显示的内容
procedure ComponentsListOnClick(Sender: TObject);
begin
if WizardForm.ComponentsList.CHECKED then
WizardForm.ComponentsList.ITEMSUBITEM:= '解压后 ' + intTOstr(size1 div 1024) + ' KB';
if not WizardForm.ComponentsList.ITEMENABLED then
WizardForm.ComponentsList.ITEMSUBITEM:= '无安装数据';
if WizardForm.ComponentsList.CHECKED then
WizardForm.ComponentsList.ITEMSUBITEM:= '解压后 ' + intTOstr(size2 div 1024) + ' KB';
if not WizardForm.ComponentsList.ITEMENABLED then
WizardForm.ComponentsList.ITEMSUBITEM:= '无安装数据';
end;

// 初始化安装向导
procedure InitializeWizard();
begin
// 释放临时文件
ExtractTemporaryFile(ExpandConstant('unzipw32.dll'));
WizardForm.TYPESCOMBO.Visible:= false;
WizardForm.ComponentsList.Onclick:= @ComponentsListOnClick;
// 根据外部文件存在的情况确定哪些组件可用
if not FileExists(ExpandConstant('{src}\data\data1.dat')) then
begin
WizardForm.ComponentsList.ITEMENABLED:= false;
WizardForm.ComponentsList.CHECKED:= false;
WizardForm.ComponentsList.ITEMSUBITEM:= '无安装数据';
end
else
begin
WizardForm.ComponentsList.ITEMENABLED:= true;
WizardForm.ComponentsList.CHECKED:= true;
size1 := UnzipSize(PChar(ExpandConstant('{src}\data\data1.dat')), dummysize);
WizardForm.ComponentsList.ITEMSUBITEM:= '解压后 ' + intTOstr(size1 div 1024) + ' KB';
end;
if not FileExists(ExpandConstant('{src}\data\data2.dat')) then
begin
WizardForm.ComponentsList.ITEMENABLED:= false;
WizardForm.ComponentsList.CHECKED:= false;
WizardForm.ComponentsList.ITEMSUBITEM:= '无安装数据';
end
else
begin
WizardForm.ComponentsList.ITEMENABLED:= true;
WizardForm.ComponentsList.CHECKED:= true;
size2 := UnzipSize(PChar(ExpandConstant('{src}\data\data2.dat')), dummysize);
WizardForm.ComponentsList.ITEMSUBITEM:= '解压后 ' + intTOstr(size2 div 1024) + ' KB';
end;
end;

// 根据组件选择的情况从外部文件中解压组件到指定的位置,比如子目录 {app}\data1 和 {app}\data2
procedure CurStepChanged(CurStep: TSetupStep);
var n: integer;
begin
if CurStep=ssInstall then
begin
if IsComponentSelected('installdata1') then
    n:= FileUnzipEx(PChar(ExpandConstant('{src}\data\data1.dat')), PChar(ExpandConstant('{app}\data1')), '*.*');
if IsComponentSelected('installdata2') then
    n:= FileUnzipEx(PChar(ExpandConstant('{src}\data\data2.dat')), PChar(ExpandConstant('{app}\data2')), '*.*');
end;
end;

页: [1]
查看完整版本: 如何创建带外部文件的安装程序