如果您知道您正在安装在具有 .NET 4.5 和更新版本的机器上(即在 Windows 8 和更新版本上),您可以使用事件函数中的 PowerShell 和.NET 4.5ZipFile
类。CurStepChanged(ssInstall)
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
Command, AppPath, ZipName, ZipTmpPath, ZipPath: string;
ResultCode: Integer;
begin
if CurStep = ssInstall then
begin
AppPath := ExpandConstant('{app}');
if not DirExists(AppPath) then
begin
Log(Format('Application path "%s" does not exist, ' +
' probably fresh install, not backing up', [AppPath]));
end
else
begin
ZipName := 'backup.zip';
ZipTmpPath := ExpandConstant('{tmp}') + '\' + ZipName;
ZipPath := AppPath + '\' + ZipName;
if FileExists(ZipPath) then
begin
if DeleteFile(ZipPath) then
Log(Format('Archive ZIP "%s" exists, deleted', [ZipPath]))
else
RaiseException(Format(
'Archive ZIP "%s" exists, and it could not be deleted', [ZipPath]));
end;
Log(Format('Archiving application folder "%s" to temporary "%s"', [
AppPath, ZipTmpPath]));
Command :=
'Add-Type -Assembly System.IO.Compression.FileSystem ; ' +
'[System.IO.Compression.ZipFile]::CreateFromDirectory(''' +
AppPath + ''', ''' + ZipTmpPath +''')';
if Exec('powershell', '-ExecutionPolicy Bypass -command "' + Command + '"',
'', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
(ResultCode = 0) then
begin
Log(Format('Application folder "%s" archived to temporary "%s"', [
AppPath, ZipTmpPath]));
end
else
begin
RaiseException(Format('Error archiving application folder "%s" "%s"', [
AppPath, ZipTmpPath]));
end;
if FileCopy(ZipTmpPath, ZipPath, False) then
Log(Format('Copied "%s" to "%s"', [ZipTmpPath, ZipPath]))
else
RaiseException(Format('Error copying "%s" to "%s"', [
ZipTmpPath, ZipPath]));
end;
end;
end;
如果您需要支持更旧版本的 Windows,则可以使用Shell.Application
class。虽然我无法让它创建一个 ZIP 文件,但只能添加一个。一个解决方案可能是将一个空的 ZIP 文件添加到安装程序中,然后添加到它。