You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Inno Setup解压归档文件时显示自定义状态并联动进度条咨询

解决方案

替换你原有[Code]段的内容即可实现需求,修改后的完整代码如下:

[Code]
var
  TotalZipFiles: Integer;
  UnzippedCount: Integer;

procedure InitializeWizard;
begin
  ForceDirectories(ExpandConstant('{localappdata}\folder-A\app\folder-B'));
  // 调整进度条最大值为200,方便拆分50%/50%的进度占比
  WizardForm.ProgressGauge.Max := 200;
end;

const
  SHCONTCH_NOPROGRESSBOX = 4;
  SHCONTCH_RESPONDYESTOALL = 16;

// 预读取压缩包内总文件数,用于计算解压进度
function GetZipFileCount(ZipFile: string): Integer;
var
  shellobj: variant;
  SrcFldr: variant;
begin
  Result := 0;
  if FileExists(ZipFile) then
  begin
    shellobj := CreateOleObject('Shell.Application');
    SrcFldr := shellobj.NameSpace(ZipFile);
    Result := SrcFldr.Items.Count;
  end;
end;

procedure unzip(ZipFile, TargetFldr: string);
var
  shellobj: variant;
  SrcFldr, DestFldr: variant;
  Item: variant;
  i: Integer;
begin
  if not FileExists(ZipFile) then Exit;

  if not DirExists(TargetFldr) then 
    if not ForceDirectories(TargetFldr) then begin
      MsgBox('无法创建目标文件夹 '+TargetFldr, mbError, MB_OK);
      Exit;
    end;    

  shellobj := CreateOleObject('Shell.Application');
  SrcFldr := shellobj.NameSpace(ZipFile);
  DestFldr := shellobj.NameSpace(TargetFldr);
  TotalZipFiles := SrcFldr.Items.Count;
  UnzippedCount := 0;

  // 逐文件解压,同步更新进度和状态
  for i := 0 to TotalZipFiles - 1 do
  begin
    Item := SrcFldr.Items.Item(i);
    // 实时显示当前正在解压的文件名
    WizardForm.StatusLabel.Caption := '正在解压: ' + Item.Name;
    // 执行单个文件解压
    DestFldr.CopyHere(Item, SHCONTCH_NOPROGRESSBOX or SHCONTCH_RESPONDYESTOALL);
    UnzippedCount := UnzippedCount + 1;
    // 更新进度:解压阶段占总进度的50%,对应进度条100-200的区间
    WizardForm.ProgressGauge.Position := 100 + Round(UnzippedCount * 100 / TotalZipFiles);
    // 强制刷新界面避免状态卡顿
    WizardForm.Refresh;
  end;
  // 解压完成后更新状态提示
  WizardForm.StatusLabel.Caption := '解压完成,正在完成安装...';
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then 
  begin
    // 常规安装完成后进入解压阶段,此时进度条已走到100(对应总进度50%)
    unzip(ExpandConstant('{app}\example.zip'), ExpandConstant('{app}\'));
  end;
end;

核心调整说明

  • 进度拆分逻辑:将进度条最大值调整为200,常规安装的文件拷贝过程占0-100区间(对应总进度50%),解压过程占100-200区间(对应总进度50%)
  • 实时状态同步:改为逐文件解压的逻辑,每解压一个文件就更新状态文本,展示当前正在处理的文件名
  • 进度联动:每完成一个文件的解压就同步更新进度条位置,避免进度条提前满格后后台才执行解压的问题

内容的提问来源于stack exchange,提问作者Uncle Bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 12:36:03