Zip files/Directories in Groovy with AntBuilder

泪湿孤枕 提交于 2019-12-03 22:49:56

问题


I am trying to zip files and directories in Groovy using AntBuilder. I have the following code:

def ant = new AntBuilder()
ant.zip(basedir: "./Testing", destfile:"${file}.zip",includes:file.name)

This zips the file "blah.txt", but not the file "New Text Document.txt". I think the issue is the spaces. I've tried the following:

ant.zip(basedir: "./Testing", destfile:"${file}.zip",includes:"${file.name}")
ant.zip(basedir: "./Testing", destfile:"${file}.zip",includes:"\"${file.name}\"")

Neither of the above resolved the issue. I'm using Ant because it will zip directories, and I don't have access to org.apache.commons.io.compression at work.


回答1:


If you look at the docs for the ant zip task, the includes parameter is described as:

comma- or space-separated list of patterns of files that must be included

So you're right, that it is the space separator that's breaking it...

You need to use the longer route to get this to work:

new AntBuilder().zip( destFile: "${file}.zip" ) {
  fileset( dir: './Testing' ) {
    include( name:file.name )
  }
}


来源:https://stackoverflow.com/questions/14225264/zip-files-directories-in-groovy-with-antbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!