Inno安装程序将文件和文件夹复制到现有的Zip文件

时间:2014-08-26 04:12:06

标签: vbscript inno-setup

有没有办法使用Inno Setup将文件从文件夹复制到zip文件?

我的安装程序脚本创建了一个临时文件夹,我想将内容(文件和文件夹的混合)移动到现有的zip文件中。我不需要压缩或任何花哨的东西......

我想,我知道这是愚蠢的,使用FileCopy是可能的,因为(据我所知)MS Windows以复制到目录的方式处理复制到zip文件夹。

[更新1]确定 - found a vbs script that does what I want,经过一些小改动后:

Dim fso, winShell, h, MyTarget, MySource

MySource = Wscript.Arguments.Item(0)
MyTarget = Wscript.Arguments.Item(1)

Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")
Set h = fso.getFile(MyTarget)

'Wscript.Echo "Adding " & MySource & " to " & MyTarget

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items

do
    wscript.sleep 500
    max = h.size
loop while h.size > max 

Set winShell = Nothing
Set fso = Nothing

从命令行运行它,如下所示: Cmd(以管理员身份运行) C:\ MyScriptLocation \ WScript ZipScript.vbs“MySourceDirPath”“MyDestDirPath \ MyZipFile.zip”

所以现在我需要让它从Inno运行: - )

[更新2]好的 - 从Inno运行,修改了脚本:

Dim objFSO, objTxt, winShell, h
Dim myFolder, myZipFile
Const ForWriting = 2

myFolder = Wscript.Arguments.Item(0)
myZipFile = Wscript.Arguments.Item(1)
Wscript.Echo "Planning to add Source: " & myFolder & " to Target: " & myZipFile

Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Wscript.Echo "Creating myZipFile" & myZipFile
' Create an empty ZIP file
    Set objTxt = objFSO.OpenTextFile( myZipFile, ForWriting, True )
    objTxt.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
    objTxt.Close
    Set objTxt = Nothing

Set winShell = createObject("shell.application")
Set h = objFSO.getFile(myZipFile)
Wscript.Echo "Started Copy Process" & myZipFile
winShell.NameSpace(myZipFile).CopyHere winShell.NameSpace(myFolder).Items, true

do
    wscript.sleep 1000
    max = h.size
loop while h.size > max 

Set winShell = Nothing
Set objFSO = Nothing

我使用以下方式从Inno运行:

[Run]
Filename: "PackItUp.vbs"; Parameters: """{code:GetDataDir_S|0}\Temp"" ""{code:GetDataDir_S|0}\myZip.zip"""; WorkingDir: "{code:GetDataDir_S|0}"; Flags: shellexec runascurrentuser

这似乎有效,但我不确定这是否是一个很好的解决方案。

1 个答案:

答案 0 :(得分:4)

这是我从未听说过的一个很好的功能。 Folder对象CopyHere方法可以将文件或文件夹附加到现有存档文件。使用后期绑定,您可以尽可能少地编写(即使您的问题中的VB脚本过于复杂,因为此方法也接受以字符串形式传递的文件名):

procedure CopyToArchive(const Archive, Content: string);
var
  Shell: Variant;
  Folder: Variant;
begin
  Shell := CreateOleObject('Shell.Application');
  Folder := Shell.NameSpace(Archive);
  Folder.CopyHere(Content);
end;

上述函数中的Archive参数必须是现有存档文件的名称(我只测试了ZIP存档,因为我不知道Windows Shell支持的其他格式) 。 Content参数可以是要包含在给定存档文件中的文件或文件夹的名称。用法可以是这样的。第一个调用显示如何将文件复制到存档,第二个调用如何复制文件夹:

CopyToArchive('C:\ExistingArchive.zip', 'C:\FileToCopy.txt');
CopyToArchive('C:\ExistingArchive.zip', 'C:\FolderToCopy\');

CopyHere方法还提供了一组选项,您可以通过这些选项优化此方法的行为。由于我不确定哪些选项可用于归档,因为有很多组合,我只是在这里发布他们的翻译以及如何使用它们保持其(可选)用法的方式您(有关更多信息,请参阅链接的MSDN参考):

[Code]
const
  FOF_SILENT = $0004; // do not display a progress dialog box
  FOF_RENAMEONCOLLISION = $0008; // give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists
  FOF_NOCONFIRMATION = $0010; // respond with "Yes to All" for any dialog box that is displayed
  FOF_ALLOWUNDO = $0040; // preserve undo information, if possible
  FOF_FILESONLY = $0080; // perform the operation on files only if a wildcard file name (*.*) is specified
  FOF_SIMPLEPROGRESS = $0100; // display a progress dialog box but do not show the file names
  FOF_NOCONFIRMMKDIR = $0200; // do not confirm the creation of a new directory if the operation requires one to be created
  FOF_NOERRORUI = $0400; // do not display a user interface if an error occurs
  FOF_NOCOPYSECURITYATTRIBS = $0800; // do not copy the security attributes of the file
  FOF_NORECURSION = $1000; // only operate in the local directory. Do not operate recursively into subdirectories
  FOF_NO_CONNECTED_ELEMENTS = $2000; // do not copy connected files as a group. Only copy the specified files

procedure CopyToArchive(const Archive, Content: string);
var
  Shell: Variant;
  Folder: Variant;
begin
  Shell := CreateOleObject('Shell.Application');
  Folder := Shell.NameSpace(Archive);
  // this will display the progress dialog and suppress showing errors
  Folder.CopyHere(Content, FOF_SIMPLEPROGRESS or FOF_NOERRORUI);
end;