Quantcast
Channel: AlferSoft Blog » Software Development
Viewing all articles
Browse latest Browse all 13

Compress a directory in inno setup

$
0
0

Inno setup script snippet to zip a directory using Windows built-in compression

function MakeBackup(indir, outfile: String): Boolean;
var
  res: Boolean;
  fso: Variant;
  winShell: Variant;
  f: Variant;
  inObj: Variant;
  outObj: Variant;
begin
  res := FALSE;
  try
    fso := CreateOleObject('Scripting.FileSystemObject');
    winShell := CreateOleObject('shell.application');
    // create a new clean zip archive
    f := fso.CreateTextFile(outfile, TRUE);
    f.write('PK' + Chr(5) + Chr(6) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0));
    f.close;
    inObj := winShell.NameSpace(indir);
    outObj := winShell.NameSpace(outfile);
    outObj.CopyHere(inObj.Items);
    repeat
      Sleep(1000);
    until outObj.Items.Count = inObj.Items.Count;
    res := TRUE;
  except
  end;
  Result := res;
end;

Sample usage:

procedure CurStepChanged(CurStep: TSetupStep);
var
  timestamp: String;
  bkprc: Boolean;
begin
  if CurStep = ssInstall then begin
    timestamp := GetDateTimeString('yyyymmdd_hhnnss', #0, #0);
    bkprc := MakeBackup(ExpandConstant('{app}'), ExpandConstant('{app}') + '\..\' + timestamp + '.zip');
    if bkprc = FALSE then begin
      if MsgBox('Backup failed. Are you sure you want to continue?', mbConfirmation, MB_YESNO) = IDNO then
        Abort();
    end;
  end;
end;

Inspired by:
Inno Setup – zip local files prior to an update
Can Windows built-in ZIP compression be scripted?


Viewing all articles
Browse latest Browse all 13

Trending Articles