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

Viewed 152

I have large file ( needs to be extracted ) in App Directory.

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

Zip file on server is abc.zip (contains abc.data)

Looked at answers here -

Download file - https://stackoverflow.com/a/66100456/2323607

Unzip https://stackoverflow.com/a/44382324/2323607

Not sure how to integrate them together

Q1 - Only download zip abc.zip, if file abc.data doesn't exist at "{userappdata}\App" location

Q2 - Once zip file is downloaded, extract abc.data it to location "{userappdata}\App"

1 Answers

By default InnoSetup will check if a file exists. You can however, change the behavior by adding a function that does the check for you. If you don't do that, it seems InnoSetup will always check for the presence of the abc.data file in the source directory.

[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;

For the download function, you can first check if the file exists, in which case you skip the download:

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

and if the file is downloaded, then unzip the file when the download is complete:

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

Full InnoSetup example with most parts taken from the linked download code and unzip code:

[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;
Related