Skip to content

Instantly share code, notes, and snippets.

@fvicente
Last active June 8, 2018 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fvicente/c18387adf86654cfdc80 to your computer and use it in GitHub Desktop.
Save fvicente/c18387adf86654cfdc80 to your computer and use it in GitHub Desktop.
Compress a directory in inno setup
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment