使用 AntBuilder 压缩特定文件

Posted

技术标签:

【中文标题】使用 AntBuilder 压缩特定文件【英文标题】:Zip specific files with AntBuilder 【发布时间】:2013-02-05 09:28:42 【问题描述】:

我需要将来自不同位置的一组文件压缩到一个 zip 中,同时保持它们的初始关系。例如,我只需要以下文件夹结构中的 a1 和 b2

Top -- A -- a1
         -- a2
    -- B -- b1
            b2

我希望 zip 文件看起来像:

Top -- A -- a1
    -- B -- b2

如何使用 AntBuilder 做到这一点? 我试过了:

def 部署文件 = [ "$HOME/Songs/a.tsv", "$HOME/Songs/b.tsv", ]

def ant = new AntBuilder()

def zipFile = new File("deployment_zipFile.zip")

ant.zip( destFile: "$zipFile.getAbsolutePath()" ) 文件集(目录:“$HOME”) 部署文件.each f -> 包括:deploymentFiles.join(",")

但这只是压缩了整个 HOME 文件夹。

【问题讨论】:

【参考方案1】:

给定这样的目录结构:

-- home
   |-- Songs
   |   |-- A
       |   |-- a1.tsv
       |   \-- a2.tsv
       |-- B
           |-- b1.tsv
           \-- b2.tsv

那么,这段代码:

def HOME = 'home'
def deploymentFiles = [ 'Songs/A/a1.tsv', 'Songs/B/b1.tsv' ]
def zipFile = new File("deployment_zipFile.zip")
new AntBuilder().zip( basedir: HOME,
                      destFile: zipFile.absolutePath,
                      includes: deploymentFiles.join( ' ' ) )

创建一个 zip 文件,提取时包含:

unzip ../deployment_zipFile.zip 
Archive:  ../deployment_zipFile.zip
   creating: Songs/
   creating: Songs/A/
  inflating: Songs/A/a1.tsv          
   creating: Songs/B/
  inflating: Songs/B/b1.tsv  

【讨论】:

以上是关于使用 AntBuilder 压缩特定文件的主要内容,如果未能解决你的问题,请参考以下文章