使用 Groovy 解压存档

发布于 2024-07-15 06:00:30 字数 114 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

酒中人 2024-07-22 06:00:30

也许 Groovy 没有对 zip 文件的“本机”支持,但使用它们仍然非常简单。

我正在处理 zip 文件,以下是我正在使用的一些逻辑:

def zipFile = new java.util.zip.ZipFile(new File('some.zip'))

zipFile.entries().each {
   println zipFile.getInputStream(it).text
}

您可以使用 findAll 方法添加其他逻辑:

def zipFile = new java.util.zip.ZipFile(new File('some.zip'))

zipFile.entries().findAll { !it.directory }.each {
   println zipFile.getInputStream(it).text
}

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:

def zipFile = new java.util.zip.ZipFile(new File('some.zip'))

zipFile.entries().each {
   println zipFile.getInputStream(it).text
}

You can add additional logic using a findAll method:

def zipFile = new java.util.zip.ZipFile(new File('some.zip'))

zipFile.entries().findAll { !it.directory }.each {
   println zipFile.getInputStream(it).text
}
孤云独去闲 2024-07-22 06:00:30

根据我的经验,最好的方法是使用 Antbuilder:

def ant = new AntBuilder()   // create an antbuilder

ant.unzip(  src:"your-src.zip",
            dest:"your-dest-directory",
            overwrite:"false" )

这样您就不必负责完成所有复杂的事情 - ant 会为您处理好。 显然,如果您需要更精细的东西,那么这行不通,但对于大多数“只需解压缩此文件”的情况来说,这确实很有效。

要使用 antbuilder,只需在类路径中包含 ant.jar 和 ant-launcher.jar 即可。

In my experience, the best way to do this is to use the Antbuilder:

def ant = new AntBuilder()   // create an antbuilder

ant.unzip(  src:"your-src.zip",
            dest:"your-dest-directory",
            overwrite:"false" )

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.

夏雨凉 2024-07-22 06:00:30

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.

吃颗糖壮壮胆 2024-07-22 06:00:30

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

零度℉ 2024-07-22 06:00:30

以下常规方法将解压缩到特定文件夹(C:\folder)。 希望这可以帮助。

import org.apache.commons.io.FileUtils
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile

def unzipFile(File file) {
    cleanupFolder()
    def zipFile = new ZipFile(file)
    zipFile.entries().each { it ->
        def path = Paths.get('c:\\folder\\' + it.name)
        if(it.directory){
            Files.createDirectories(path)
        }
        else {
            def parentDir = path.getParent()
            if (!Files.exists(parentDir)) {
                Files.createDirectories(parentDir)
            }
            Files.copy(zipFile.getInputStream(it), path)
        }
    }
}

private cleanupFolder() {
    FileUtils.deleteDirectory(new File('c:\\folder\\'))
}

The below groovy methods will unzip into specific folder (C:\folder). Hope this helps.

import org.apache.commons.io.FileUtils
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile

def unzipFile(File file) {
    cleanupFolder()
    def zipFile = new ZipFile(file)
    zipFile.entries().each { it ->
        def path = Paths.get('c:\\folder\\' + it.name)
        if(it.directory){
            Files.createDirectories(path)
        }
        else {
            def parentDir = path.getParent()
            if (!Files.exists(parentDir)) {
                Files.createDirectories(parentDir)
            }
            Files.copy(zipFile.getInputStream(it), path)
        }
    }
}

private cleanupFolder() {
    FileUtils.deleteDirectory(new File('c:\\folder\\'))
}
好久不见√ 2024-07-22 06:00:30

本文对 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

如果没有 2024-07-22 06:00:30

虽然从另一个方向考虑这个问题,但我开始使用 Groovy 来构建我正在构建的 DSL,但最终使用 Gradle 作为起点,以更好地处理我想做的许多基于文件的任务(例如.、解压缩和解压文件、执行其他程序等)。 Gradle 建立在 groovy 功能​​的基础上,并且可以通过插件进一步扩展。

// build.gradle
task doUnTar << {
    copy {
        // tarTree uses file ext to guess compression, or may be specific
        from tarTree(resources.gzip('foo.tar.gz'))
        into getBuildDir()
    }
}

task doUnZip << {
    copy {
        from zipTree('bar.zip')
        into getBuildDir()
    }
}

然后,例如(这会将 bar.zipfoo.tgz 提取到目录 build 中):

$ gradle doUnZip
$ gradle doUnTar

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.

// build.gradle
task doUnTar << {
    copy {
        // tarTree uses file ext to guess compression, or may be specific
        from tarTree(resources.gzip('foo.tar.gz'))
        into getBuildDir()
    }
}

task doUnZip << {
    copy {
        from zipTree('bar.zip')
        into getBuildDir()
    }
}

Then, for example (this extracts the bar.zip and foo.tgz into the directory build):

$ gradle doUnZip
$ gradle doUnTar
若沐 2024-07-22 06:00:30
def zip(String s){
    def targetStream = new ByteArrayOutputStream()
    def zipStream = new GZIPOutputStream(targetStream)
    zipStream.write(s.getBytes())
    zipStream.close()
    def zipped = targetStream.toByteArray()
    targetStream.close()
    return zipped.encodeBase64()
}
def zip(String s){
    def targetStream = new ByteArrayOutputStream()
    def zipStream = new GZIPOutputStream(targetStream)
    zipStream.write(s.getBytes())
    zipStream.close()
    def zipped = targetStream.toByteArray()
    targetStream.close()
    return zipped.encodeBase64()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文