2012-10-16 117 views
5

我需要从本地计算机复制zip文件并粘贴到远程计算机,然后将这些文件解压缩到远程计算机上。将文件复制并解压缩到远程计算机 - ant

我知道第一部分可以使用scp(从本地拷贝zip文件并粘贴到远程机器)完成,但如何做第二部分使用ant?

在此先感谢

回答

4

您可以使用sshexec task调用远程计算机上的命令行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 '" /> 

unzip命令可以采取一些其他选项,请其man page的全部细节。例如,-j选项将忽略zip文件内的任何目录层次结构,并将所有提取的文件直接放在目标目录中。并且-o将强制覆盖目标目录中的现有文件而不提示。

+0

你能给我举个例子,我需要解压缩一个特定目录中的所有文件,并将这些解压缩的文件放在其他目录中使用sshexec? – coolgokul

+0

@coolgokul我已经添加了一个(希望全面的)例子。 –

+0

太棒了。它的工作正常。两个问题。 1.如何使该程序解压缩一个文件夹中的所有文件并将提取的文件移动到一个目录中。 2.如果文件已经在目标目录中解压缩,并且如果我尝试再次解压缩,它会要求替换文件?如何总是设置为更换文件是?提前致谢。 – coolgokul

相关问题