Inno Setup:删除先前版本安装的文件

5

我正在使用Inno Setup将一款Java应用打包为Windows可执行程序,应用程序的目录结构如下:

|   MyApp.jar
\---lib
    |   dependency-A-1.2.3.jar
    |   dependency-B-2.3.4.jar
    |   dependency-Z-x.y.z.jar

我使用Ant来预先准备整个目录树(所有的文件和文件夹),包括lib目录(使用*.jar通配符复制依赖项),然后我只需使用以下命令调用ISCC

[Files]
Source: "PreparedFolder\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs

现在,每当用户升级应用程序时,我需要清理lib目录,因为我想删除任何过时的依赖项。我可以在我的.iss文件中添加以下部分:

[InstallDelete]
{app}\lib\*.jar

但我感到不安全,因为如果用户决定将应用安装在包含非空 lib 子文件夹的现有文件夹中(这很少见但并非不可能),则升级时会有删除一些用户文件的风险。

有没有什么最佳实践可以避免这种麻烦?其他安装程序是否已经解决了这些问题?谢谢。

2个回答

5
您可以在安装之前卸载以前的版本:

如果无法进行完全卸载,则需要实现部分卸载。

理想情况下,应该对卸载程序日志(unins000.dat)进行反向工程,仅提取到lib子文件夹中的安装内容并进行处理(撤消)。但由于该文件是未经记录的二进制文件,因此可能难以实现。


如果您在 [Files] 部分中维护了要安装的文件的显式列表,比如:

[Files]
Source: "lib\dependency-A-1.2.3.jar"; Dest: "{app}\lib"
Source: "lib\dependency-B-2.3.4.jar"; Dest: "{app}\lib"

每当依赖项发生更改时,将先前的版本移动到 [InstallDelete] 部分:

[Files]
Source: "lib\dependency-A-1.3.0.jar"; Dest: "{app}"
Source: "lib\dependency-B-2.3.4.jar"; Dest: "{app}"

[InstallDelete]
{app}\lib\dependency-A-1.2.3.jar

如果您使用通配符安装依赖项,
[Files]
Source: "lib\*.jar"; Dest: "{app}\lib"

您无法反向工程卸载程序日志,您需要通过自己的手段复制其功能。

您可以使用预处理器生成一个已安装依赖项的文件。将该文件安装到{app}文件夹中,并在安装之前处理该文件。

[Files]
Source: "MyApp.jar"; DestDir: "{app}"
Source: "lib\*.jar"; DestDir: "{app}\lib"

#define ProcessFile(Source, FindResult, FindHandle) \
    Local[0] = FindGetFileName(FindHandle), \
    Local[1] = Source + "\\" + Local[0], \
    Local[2] = FindNext(FindHandle), \
    "'" + Local[0] + "'#13#10" + \
        (Local[2] ? ProcessFile(Source, Local[2], FindHandle) : "")

#define ProcessFolder(Source) \
    Local[0] = FindFirst(Source + "\\*.jar", faAnyFile), \
    ProcessFile(Source, Local[0], Local[0])

#define DepedenciesToInstall ProcessFolder("lib")
#define DependenciesLog "{app}\dependencies.log"

[UninstallDelete]
Type: files; Name: "{#DependenciesLog}"

[Code]

procedure CurStepChanged(CurStep: TSetupStep);
var
  AppPath, DependenciesLogPath: string;
  Dependencies: TArrayOfString;
  Count, I: Integer;
begin
  DependenciesLogPath := ExpandConstant('{#DependenciesLog}');

  if CurStep = ssInstall then
  begin
    // If dependencies log already exists, 
    // remove the previously installed dependencies
    if LoadStringsFromFile(DependenciesLogPath, Dependencies) then
    begin
      Count := GetArrayLength(Dependencies);
      Log(Format('Loaded %d dependencies, deleting...', [Count]));
      for I := 0 to Count - 1 do
        DeleteFile(ExpandConstant('{app}\lib\' + Dependencies[I]));
    end;
  end
    else
  if CurStep = ssPostInstall then
  begin
    // Now that the app folder already exists,
    // save dependencies log (to be processed by future upgrade)
    if SaveStringToFile(DependenciesLogPath, {#DepedenciesToInstall}, False) then
    begin
      Log('Created dependencies log');
    end
      else
    begin
      Log('Failed to create dependencies log');
    end;
  end;
end;

另一种方法是删除安装文件夹中未被最新安装程序安装的所有文件。

最简单的解决方案是在安装之前删除安装文件夹中的所有文件。

您可以使用[InstallDelete]部分来实现。但如果您的安装文件夹中有一些包含配置文件的文件夹/文件,它将不允许您将它们排除在外。

您可以编写Pascal脚本来实现。请参见Inno Setup - Delete whole application folder except for data subdirectory。您可以从我的回答中调用DelTreeExceptSavesDir函数,并将其放置在CurStepChanged(ssInstall)事件函数中:

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    DelTreeExceptSavesDir(WizardDirValue); 
  end;
end;

如果您只想删除过时的文件,以避免删除和重新创建现有文件,则可以使用预处理器生成要安装到Pascal脚本的文件列表,并使用该列表仅删除真正过时的文件。

#pragma parseroption -p-

#define FileEntry(DestDir) \
    "  FilesNotToBeDeleted.Add('" + LowerCase(DestDir) + "');\n"

#define ProcessFile(Source, Dest, FindResult, FindHandle) \
    FindResult \
        ? \
            Local[0] = FindGetFileName(FindHandle), \
            Local[1] = Source + "\\" + Local[0], \
            Local[2] = Dest + "\\" + Local[0], \
            (Local[0] != "." && Local[0] != ".." \
                ? FileEntry(Local[2]) + \
                  (DirExists(Local[1]) ? ProcessFolder(Local[1], Local[2]) : "") \
                : "") + \
            ProcessFile(Source, Dest, FindNext(FindHandle), FindHandle) \
        : \
            ""

#define ProcessFolder(Source, Dest) \
    Local[0] = FindFirst(Source + "\\*", faAnyFile), \
    ProcessFile(Source, Dest, Local[0], Local[0])

#pragma parseroption -p+

[Code]

var
  FilesNotToBeDeleted: TStringList;

function InitializeSetup(): Boolean;
begin
  FilesNotToBeDeleted := TStringList.Create;
  FilesNotToBeDeleted.Add('\data');
  {#Trim(ProcessFolder('build\exe.win-amd64-3.6', ''))}
  FilesNotToBeDeleted.Sorted := True;

  Result := True;
end;

procedure DeleteObsoleteFiles(Path: string; RelativePath: string);
var
  FindRec: TFindRec;
  FilePath: string;
  FileRelativePath: string;
begin
  if FindFirst(Path + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          FilePath := Path + '\' + FindRec.Name;
          FileRelativePath := RelativePath + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
          begin
            DeleteObsoleteFiles(FilePath, FileRelativePath);
          end;

          if FilesNotToBeDeleted.IndexOf(Lowercase(FileRelativePath)) < 0 then
          begin
            if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
            begin
              if RemoveDir(FilePath) then
              begin
                Log(Format('Deleted obsolete directory %s', [FilePath]));
              end
                else
              begin
                Log(Format('Failed to delete obsolete directory %s', [FilePath]));
              end;
            end
              else
            begin
              if DeleteFile(FilePath) then
              begin
                Log(Format('Deleted obsolete file %s', [FilePath]));
              end
                else
              begin
                Log(Format('Failed to delete obsolete file %s', [FilePath]));
              end;
            end;
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [Path]));
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    Log('Looking for obsolete files...');
    DeleteObsoleteFiles(WizardDirValue, '');
  end;
end;

1

试试这个,希望它能工作?

[InstallDelete]
Type: filesandordirs; Name: "{app}\lib\dependency-A-1.2.3.jar"

我还没有测试过,我只是从阅读文档中得到了它。 - GoodDay
它将在安装/解压缩文件之前运行。 - GoodDay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接