3

在运行我的安装程序的新版本时,我想通过将文件添加到 ZIP 存档来备份现有安装。

目前,我可以通过将文件复制到我的Backup目的地来备份现有安装。我使用的方法的简化版本如下:

[Files]
; Copy the contents of Bin folder to Backup folder. Skip if files don’t exist. 
Source: {app}\Bin\*;  DestDir: {app}\Backup\; \
    Flags: createallsubdirs external recursesubdirs uninsneveruninstall skipifsourcedoesntexist;

我会很感激任何想法我可以如何压缩?

4

3 回答 3

2

您可能想查看 LOGAN ISSI: http: //members.home.nl/albartus/inno/index.html 有一些工具与它捆绑在一起。

另一种方法是包含一个批处理文件,该文件将执行备份步骤,然后在完成后将其自行删除。

结帐此线程:批处理脚本以压缩没有父文件夹的所有文件

但是,由于许可,您不允许将 rar.dll 与您的应用程序捆绑,因此只需应用此方法,但使用可以重新分发的包/dll。

如果您可以使用 VBScript 编写一个简单的包装器,您也可以使用内置 zip 压缩的 windows。

看看你如何:可以编写 Windows 的内置 ZIP 压缩脚本吗?

于 2013-11-10T18:34:18.827 回答
2

我使用了 TLama 的建议,并在 Delphi (XE3) 中创建了自己的 DLL,用于压缩文件夹。

library MyZipLib;
uses
  Winapi.Windows,
  System.SysUtils,
  System.Zip;

{$R *.res}

function ZipCompressFolder(SourcePath, DestinationPath, ArchiveName : PChar): boolean; stdcall;
begin
  //If source folder does not exist then exit without error.
  if not DirectoryExists(SourcePath) then
  begin
    result := true;
    exit;
  end;

  try
    result := false;

    //Make sure destination path exists
    ForceDirectories(DestinationPath);
    TZipFile.ZipDirectoryContents(IncludeTrailingPathDelimiter(DestinationPath) + ArchiveName, SourcePath);
    result := true;
  except
    on E : Exception do MessageBox(0, PChar('Error calling function ZipCompressFolder@MyZipLib.dll with message: ' + E.Message + #13#10 + 'Source: ' + SourcePath + #13#10 + 'Dest: ' + DestinationPath+ #13#10 + 'Archive: ' + ArchiveName), 'MyZipLib.dll Error', MB_OK);
  end;
end;

exports ZipCompressFolder;
begin
end.
于 2013-11-11T01:14:34.757 回答
1

如果您知道您正在安装在具有 .NET 4.5 和更新版本的机器上(即在 Windows 8 和更新版本上),您可以使用事件函数中的 PowerShell 和.NET 4.5ZipFileCurStepChanged(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.Applicationclass。虽然我无法让它创建一个 ZIP 文件,但只能添加一个。一个解决方案可能是将一个空的 ZIP 文件添加到安装程序中,然后添加到它。

于 2021-11-23T11:09:00.683 回答