Inno Setup将文件和文件夹复制到现有的Zip文件 [英] Inno Setup Copy Files and Folders to an Existing Zip File
问题描述
是否可以使用Inno Setup将文件从文件夹复制到zip文件?
Is there a way to copy files from a folder to a zip file using Inno Setup?
我的安装程序脚本创建了一个临时文件夹,我想将内容(文件和文件夹的混合)移动到现有的zip文件中.我不需要压缩或任何幻想...
My installer script creates a Temp Folder and I would like to move the content (mix of files and folders) to an existing zip file. I don't need compression or anything fancy...
我认为而且我知道这很愚蠢,使用FileCopy可以实现这一点,因为(据我所知)MS Windows将复制到zip文件夹的方式与复制到目录的方式类似.
I thought, and I know this is dumb, that this would be possible using FileCopy because (as far as I know) MS Windows treats copying to a zip folder in a similar way as copying to a directory.
[更新1]好-找到了vbs经过一些小的更改后,可以执行我想要的脚本:
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"
And ran it from command line like this: Cmd (run as admin) C:\MyScriptLocation\WScript ZipScript.vbs "MySourceDirPath" "MyDestDirPath\MyZipFile.zip"
所以现在我需要让它从Inno运行:-)
So now I need to get it to run from Inno :-)
[Update 2]好的-让它从Inno运行,修改了脚本:
[Update 2] Ok - got it running from Inno, revised the script:
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进行以下操作:
I am running from Inno using:
[Run]
Filename: "PackItUp.vbs"; Parameters: """{code:GetDataDir_S|0}\Temp"" ""{code:GetDataDir_S|0}\myZip.zip"""; WorkingDir: "{code:GetDataDir_S|0}"; Flags: shellexec runascurrentuser
这似乎可行,但是我不确定这是否是一个好的解决方案.
Which seems to work, but I am unsure if this is a good solution.
推荐答案
这是我从未听说过的不错的功能. Folder
对象 CopyHere
方法可以将文件或文件夹追加到现有的存档文件中.使用后期绑定,您可以编写尽可能少的内容(即使您问题中的VB脚本也过于复杂,因为此方法还接受以字符串形式传递的文件名):
This is a nice feature that I've never heard about. The Folder
object CopyHere
method can append files or folders to existing archive files. With late binding you can write as little as this (even the VB script in your question was overcomplicated since this method accepts also file name passed as string):
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
参数必须是现有存档文件的名称(由于我不知道Windows Shell支持这种格式的其他格式,所以我仅测试了ZIP存档). Content
参数可以是要包含在给定存档文件中的文件或文件夹的名称.用法可以像这样.第一个调用显示如何将文件复制到存档,第二个显示如何复制文件夹:
The Archive
parameter in the above function must be the name of an existing archive file (I've tested only ZIP archive since I don't know what other formats Windows Shell supports this way). The Content
parameter can be the name of the file or folder that you want to include to the given archive file. Usage can be like this. The first call shows how to copy file to the archive, the second how to copy folder:
CopyToArchive('C:\ExistingArchive.zip', 'C:\FileToCopy.txt');
CopyToArchive('C:\ExistingArchive.zip', 'C:\FolderToCopy\');
CopyHere
方法还提供了一组选项,通过它们可以优化此方法的行为.由于我不确定哪些选项可用于存档,并且由于存在很多组合,因此我将在此处发布其翻译以及如何使用它们以保持其(可选)用法的方式(有关更多信息,请参见链接的MSDN参考):
The CopyHere
method also provides a set of options, by which you can refine behavior of this method. Since I'm not sure which options can be used for archives and since there's a lot of combinations, I'll just post here their translation and the way how to use them keeping their (optional) usage on you (for more information see the linked MSDN reference):
[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;
这篇关于Inno Setup将文件和文件夹复制到现有的Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!