如何让Inno Setup安装完成后生成zip包存储至AppData/local
实现方法
核心逻辑是在Inno Setup的安装完成回调阶段调用压缩能力,把安装目录的文件打包到目标路径,不需要额外修改你的Python应用代码,直接改Inno Setup的.iss脚本即可。
方案一:调用系统PowerShell压缩(推荐,适配Win10及以上系统)
Win10及以上版本的系统自带PowerShell的压缩命令,不需要额外打包任何工具,直接加以下代码到你的.iss脚本末尾即可:
[Code] procedure CurStepChanged(CurStep: TSetupStep); var PowerShellPath: string; CommandLine: string; SourceDir: string; OutputZipPath: string; ResultCode: Integer; begin // 仅在所有文件安装完成后执行压缩逻辑 if CurStep = ssPostInstall then begin // 要打包的源:安装目录下所有文件 SourceDir := ExpandConstant('{app}\*'); // 压缩包输出路径:LocalAppData下,可自行修改压缩包名称 OutputZipPath := ExpandConstant('{localappdata}\你的Python应用安装包.zip'); // 定位PowerShell程序路径 PowerShellPath := ExpandConstant('{sys}\WindowsPowerShell\v1.0\powershell.exe'); // 拼接压缩命令,-Force参数表示覆盖已存在的同名压缩包 CommandLine := '-NoProfile -ExecutionPolicy Bypass -Command "Compress-Archive -Path ''' + SourceDir + ''' -DestinationPath ''' + OutputZipPath + ''' -Force"'; // 后台执行命令,等待压缩完成再结束安装流程 Exec(PowerShellPath, CommandLine, '', SW_HIDE, ewWaitUntilTerminated, ResultCode); end; end;
注意:如果安装目录文件体积较大,压缩会消耗几秒时间,安装程序最后一步会等待压缩完成再退出,属于正常现象
方案二:自带7zip命令行工具(兼容Win7等旧系统)
如果需要适配Win7这类没有内置PowerShell压缩命令的旧系统,可以把体积仅几百KB的7za.exe(7zip的独立命令行版本)加入安装包,调用它完成压缩:
- 先在[Files]段添加7za.exe的安装配置:
[Files] Source: "你本地存放7za.exe的路径"; DestDir: "{app}"; Flags: ignoreversion
- 再添加和修改[Code]段逻辑:
[Code] procedure CurStepChanged(CurStep: TSetupStep); var SevenZipPath: string; CommandLine: string; SourceDir: string; OutputZipPath: string; ResultCode: Integer; begin if CurStep = ssPostInstall then begin SourceDir := ExpandConstant('{app}\*'); OutputZipPath := ExpandConstant('{localappdata}\你的Python应用安装包.zip'); SevenZipPath := ExpandConstant('{app}\7za.exe'); // 7za压缩命令,-y参数表示自动覆盖同名文件 CommandLine := 'a -tzip "' + OutputZipPath + '" "' + SourceDir + '" -y'; Exec(SevenZipPath, CommandLine, '', SW_HIDE, ewWaitUntilTerminated, ResultCode); // 不需要保留7za.exe的话可以执行完删除 DeleteFile(SevenZipPath); end; end;
验证方式
编译生成安装包后执行安装,完成后打开资源管理器输入%localappdata%回车,就能看到生成的zip压缩包,解压后内容和安装目录完全一致。
内容的提问来源于stack exchange,提问作者Uncle Bob
相关产品推荐
相关产品推荐

