【问题标题】:copy and unzip files to remote machine - ant将文件复制并解压缩到远程机器 - ant
【发布时间】:2012-10-06 23:50:09
【问题描述】:

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

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

提前致谢

【问题讨论】:

    标签: ant ssh exec scp


    【解决方案1】:

    您可以使用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会在不提示的情况下强制覆盖目标目录下已有的文件。

    【讨论】:

    • 你能给我举个例子,我需要解压缩一个特定目录中的所有文件,然后使用 sshexec 将这些解压缩文件放在另一个目录中吗?
    • 太棒了。它工作正常。两个问题。 1.如何使这个程序解压一个文件夹中的所有文件,并将提取的文件移动到一个目录中。? 2. 如果文件已经在目标目录中解压缩并且我尝试再次解压缩它要求替换文件?如何始终设置是来替换文件?提前致谢。
    • @coolgokul 我不完全确定您所说的 1 是什么意思,但我添加了一些可能会有所帮助的细节。
    • 真棒.. 工作得很好.. 我的第一个问题是,假设我有一个名为“sources”的文件夹,其中有许多 zip 文件。 (file1.zip、abc.zip、123.zip 和特殊字符命名文件夹等)...现在我需要解压缩源文件夹中的所有文件,并将所有解压缩的文件放入“Temp”文件夹。你知道怎么做吗?
    • 我认为这就是我上面的代码将执行的操作 - 您的“源”目录是 archives 属性(包含本地原始 zip 文件的目录),您的“临时”文件夹是 @ 987654329@(服务器上从这些文件中解压缩的内容将结束的文件夹)。如果您希望所有解压缩文件直接在“Temp”而不是子目录中结束,那么您只需添加-j,即unzip -j -d ${unzip.destination} ....
    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2021-03-08
    • 2011-11-01
    • 1970-01-01
    相关资源
    最近更新 更多