使用 Groovy 解压存档
Groovy 是否有内置支持来处理 Zip 文件(groovy 方式)?
或者我是否必须使用 Java 的 java.util.zip.ZipFile 来处理 Groovy 中的 Zip 文件?
is there a built-in support in Groovy to handle Zip files (the groovy way)?
Or do i have to use Java's java.util.zip.ZipFile to process Zip files in Groovy ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
也许 Groovy 没有对 zip 文件的“本机”支持,但使用它们仍然非常简单。
我正在处理 zip 文件,以下是我正在使用的一些逻辑:
您可以使用
findAll
方法添加其他逻辑:Maybe Groovy doesn't have 'native' support for zip files, but it is still pretty trivial to work with them.
I'm working with zip files and the following is some of the logic I'm using:
You can add additional logic using a
findAll
method:根据我的经验,最好的方法是使用 Antbuilder:
这样您就不必负责完成所有复杂的事情 - ant 会为您处理好。 显然,如果您需要更精细的东西,那么这行不通,但对于大多数“只需解压缩此文件”的情况来说,这确实很有效。
要使用 antbuilder,只需在类路径中包含 ant.jar 和 ant-launcher.jar 即可。
In my experience, the best way to do this is to use the Antbuilder:
This way you aren't responsible for doing all the complicated stuff - ant takes care of it for you. Obviously if you need something more granular then this isn't going to work, but for most 'just unzip this file' scenarios this is really effective.
To use antbuilder, just include ant.jar and ant-launcher.jar in your classpath.
AFAIK,没有本地方法。 但请查看文章 强大的 Groovy 介绍了如何向文件添加
.zip(...)
方法,这将非常接近您正在寻找的内容。 您只需要创建一个.unzip(...)
方法。AFAIK, there isn't a native way. But check out the article Powerful Groovy on how you'd add a
.zip(...)
method to File, which would be very close to what you're looking for. You'd just need to make an.unzip(...)
method.Groovy 通用扩展项目为 Groovy 2.0 及更高版本提供此功能: https://github.com/timyates /groovy-common-extensions
The Groovy common extension project provides this functionality for Groovy 2.0 and above: https://github.com/timyates/groovy-common-extensions
以下常规方法将解压缩到特定文件夹(C:\folder)。 希望这可以帮助。
The below groovy methods will unzip into specific folder (C:\folder). Hope this helps.
本文对 AntBuilder 示例进行了扩展。
http://preferisco.blogspot.com/ 2010/06/using-goovy-antbuilder-to-zip-unzip.html
但是,作为一个原则问题 - 有没有办法找出可以在以下情况下使用的所有属性、闭包、映射等研究 groovy/java 的新方面?
似乎有很多真正有用的东西,但如何解锁它们隐藏的宝藏呢? 现在,NetBeans/Eclipse 代码完整功能似乎完全受限于我们这里拥有的新语言丰富性。
This article expands on the AntBuilder example.
http://preferisco.blogspot.com/2010/06/using-goovy-antbuilder-to-zip-unzip.html
However, as a matter of principal - is there a way to find out all of the properties, closures, maps etc that can be used when researching a new facet in groovy/java?
There seem to be loads of really useful things, but how to unlock their hidden treasures? The NetBeans/Eclipse code-complete features now seem hopelessly limited in the new language richness that we have here.
使用 AntBuilder 解压是个好方法。
第二个选择是使用第三方库 - 我推荐 Zip4j
Unzip using AntBuilder is good way.
Second option is use an third party library - I recommend Zip4j
虽然从另一个方向考虑这个问题,但我开始使用 Groovy 来构建我正在构建的 DSL,但最终使用 Gradle 作为起点,以更好地处理我想做的许多基于文件的任务(例如.、解压缩和解压文件、执行其他程序等)。 Gradle 建立在 groovy 功能的基础上,并且可以通过插件进一步扩展。
然后,例如(这会将
bar.zip
和foo.tgz
提取到目录build
中):Although taking the question a bit into another direction, I started off using Groovy for a DSL that I was building, but ended up using Gradle as a starting point to better handle a lot of the file-based tasks that I wanted to do (eg., unzip and untar files, execute other programs, etc). Gradle builds on what groovy can do, and can be extended further via plugins.
Then, for example (this extracts the
bar.zip
andfoo.tgz
into the directorybuild
):