Inno Setup:检查提取的文件是否存在,如果不存在则下载 zip 文件并提取

Inno Setup: Check if extracted file exists, if not download zip file and extract

我在 App Directory 中有大文件(需要提取)。

[Files]
Source: "Installer Files\out\abc.data"; DestDir: "{userappdata}\App"; Flags: ignoreversion onlyifdoesntexist

服务器上的 Zip 文件是 abc.zip(包含 abc.data)

在这里查看答案 -

下载文件 -

解压

不确定如何将它们整合在一起[=13​​=]

Q1 - 仅下载 zip abc.zip,如果文件 abc.data 不存在于“{ userappdata}\App" 位置

Q2 - 下载 zip 文件后,将其解压缩 abc.data 到位置“{userappdata}\App”。 =13=]

默认情况下,InnoSetup 将检查文件是否存在。但是,您可以通过添加一个为您进行检查的函数来更改行为。如果你不这样做,InnoSetup 似乎总是会检查源目录中是否存在 abc.data 文件。

[Files]
Source: "{tmp}\abc.data"; DestDir: "{userappdata}\App"; Flags: external; Check: ExtractedFileNeedsInstallation


[Code]
function ExtractedFileNeedsInstallation: Boolean;
var 
  TargetPath: String; 
begin  
  TargetPath := ExpandConstant('{userappdata}')+'\App\abc.data';
  Result := not FileExists(TargetPath);
  Log(Format('ExtractedFileNeedsInstallation: %d', [Result]));  
end;

对于下载功能,可以先检查文件是否存在,如果存在则跳过下载:

  if CurPageID = wpReady then begin
      if (not ExtractedFileNeedsInstallation) then
      begin
          Result := True;
      end
      else

如果文件已下载,则在下载完成后解压缩文件:

try
  DownloadPage.Download;
  Temp := ExpandConstant('{tmp}');
  UnZip(Temp+'\abc.zip', 'abc.data', Temp);
  Result := True;
except

完整的 InnoSetup 示例,其中大部分来自链接 download code and :

[Setup]
AppName=DownloadExample
AppVersion=1.0
DefaultDirName=DownloadTest
[Files]
Source: "{tmp}\abc.data"; DestDir: "{userappdata}\App"; Flags: external; Check: ExtractedFileNeedsInstallation

[Code]
const
  NO_PROGRESS_BOX = 4;
  RESPOND_YES_TO_ALL = 16;
procedure UnZip(ZipPath, FileName, TargetPath: string); 
var
  Shell: Variant;
  ZipFile: Variant;
  Item: Variant;
  TargetFolder: Variant;
begin
  Shell := CreateOleObject('Shell.Application');

  ZipFile := Shell.NameSpace(ZipPath);
  if VarIsClear(ZipFile) then
    RaiseException(Format('Cannot open ZIP file "%s" or does not exist', [ZipPath]));

  Item := ZipFile.ParseName(FileName);
  if VarIsClear(Item) then
    RaiseException(Format('Cannot find "%s" in "%s" ZIP file', [FileName, ZipPath]));

  TargetFolder := Shell.NameSpace(TargetPath);
  if VarIsClear(TargetFolder) then
    RaiseException(Format('Target path "%s" does not exist', [TargetPath]));

  TargetFolder.CopyHere(Item, NO_PROGRESS_BOX or RESPOND_YES_TO_ALL);
end;

function ExtractedFileNeedsInstallation: Boolean;
var 
  TargetPath: String; 
begin  
  TargetPath := ExpandConstant('{userappdata}')+'\App\abc.data';
  Result := not FileExists(TargetPath);
  Log(Format('ExtractedFileNeedsInstallation: %d', [Result]));  
end;

var
  DownloadPage: TDownloadWizardPage;

function OnDownloadProgress(const Url, FileName: String; const Progress, ProgressMax: Int64): Boolean;

begin
  if Progress = ProgressMax then
  begin
    Log(Format('Successfully downloaded file to {tmp}: %s', [FileName]));
  end;
  Result := True;
end;

procedure InitializeWizard;
begin
  DownloadPage := CreateDownloadPage(SetupMessage(msgWizardPreparing), SetupMessage(msgPreparingDesc), @OnDownloadProgress);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var  
  Temp: String;
begin
  if CurPageID = wpReady then begin
      if (not ExtractedFileNeedsInstallation) then
      begin
          Result := True;
      end
      else
      begin
          DownloadPage.Clear;
          DownloadPage.Add('http://37.120.179.6/test/thomas/upload/abc.zip', 'abc.zip', '');
          DownloadPage.Show;
          try
            try
              DownloadPage.Download;
              Temp := ExpandConstant('{tmp}');
              UnZip(Temp+'\abc.zip', 'abc.data', Temp);
              Result := True;
            except
              SuppressibleMsgBox(AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
              Result := False;
            end;
          finally
            DownloadPage.Hide;
          end;
      end;
  end else
    Result := True;
end;