将文件复制并解压缩到远程机器 - 蚂蚁

coo*_*kul 5 ant ssh scp exec

我需要从本地机器复制zip文件并粘贴到远程机器中并将这些文件解压缩到远程机器中.

我知道第一部分可以使用scp(从本地复制zip文件并粘贴在远程机器中)但是如何使用ant进行第二部分?

提前致谢

Ian*_*rts 4

您可以使用sshexec 任务在远程计算机上调用命令行unzip命令(假设远程计算机已安装 unzip)。

<!-- local directory containing the files to copy -->
<property name="archives" location="C:\path\to\zipfiles" />
<property name="archives.destination" value="/home/testuser/archives" />
<property name="unzip.destination" value="/home/testuser/unpacked" />

<fileset id="zipfiles.to.copy" dir="${archives}" includes="*.zip" />

<!-- copy the archives to the remote server -->
<scp todir="${user}:${password}@host.example.com:${archives.destination}">
  <fileset refid="zipfiles.to.copy" />
</scp>

<!-- Build the command line for unzip - the idea here is to turn the local
     paths into the corresponding paths on the remote, i.e. to turn
     C:\path\to\zipfiles\file1.zip;C:\path\to\zipfiles\file2.zip... into
     /home/testuser/archives/file1.zip /home/testuser/archives/file2.zip

     For this to work there must be no spaces in any of the zipfile names.
-->
<pathconvert dirsep="/" pathsep=" " property="unzip.files" refid="zipfiles.to.copy">
  <map from="${archives}" to="${archives.destination}" />
</pathconvert>

<!-- execute the command.  Use the "-d" option to unzip so it will work
     whatever the "current" directory on the remote side -->
<sshexec host="host.example.com" username="${user}" password="${password}"
  command="/bin/sh -c '
    for zipfile in ${unzip.files}; do
      /usr/bin/unzip -d ${unzip.destination} $$zipfile ; done '" />
Run Code Online (Sandbox Code Playgroud)

unzip命令可以采用许多其他选项,请参阅其手册页以获取完整的详细信息。例如,该-j选项将忽略 zip 文件内的任何目录层次结构,并将所有提取的文件直接放入目标目录中。并-o会在没有提示的情况下强制覆盖目标目录中的现有文件。