使用 TCL tk_getOpenFile 压缩多个文件

Zipping multiple files using TCL's tk_getOpenFile

我正在用 tcl 创建一个打包器,用户可以从中 select 他想将哪些文件压缩到包中。

问题是,我无法压缩多个文件,当多个文件被 selected 时,$file 变量中的路径会变成这样:“/home/file.txt /home/file2.txt" 因为路径不喜欢,所以无法压缩。

有没有办法使用tk_getOpenFile函数压缩多个文件?

set types {
    {{Text Files}       {.txt}        }  
 }
 #Here i'm defining that only .txt files can be selected

set file [tk_getOpenFile -multiple 1 -filetypes $types -parent .] 
#the file chooser function in tcl, where it can be chosen multiple files at once, the path of the files go into the $file variable

exec zip -j package.zip $file 
# A shell script function for zipping the files
exec zip -j package.zip {*}$file

如果您使用的是 Tcl 8.5 或更高版本。

{*} 前缀重写了调用,这样如果它后面的表达式是一个列表,则该列表中的每个元素都成为调用中的一个词。如果 $foo 的值为 {a b c} 而您调用 bar abc {*}$foo def,则实际调用变为 bar abc a b c def.

只要您保持 -multiple 选项打开,您应该不会对包含空格的文件名有任何问题。

文档:{*}