2012-10-16 119 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

相關問題