Java 使用 Groovy 解压缩存档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/645847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:20:29  来源:igfitidea点击:

Unzip Archive with Groovy

javagroovyzipunzip

提问by HaBaLeS

is there a built-in support in Groovy to handle Zip files (the groovy way)?

Groovy 中是否有内置支持来处理 Zip 文件(groovy 方式)?

Or do i have to use Java's java.util.zip.ZipFile to process Zip files in Groovy ?

还是我必须使用 Java 的 java.util.zip.ZipFile 来处理 Groovy 中的 Zip 文件?

采纳答案by John Feminella

AFAIK, there isn't a native way. But check out this articleon 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.

AFAIK,没有本地方式。但是请查看这篇关于如何.zip(...)向 File添加方法的文章,这与您要查找的内容非常接近。你只需要制定一个.unzip(...)方法。

回答by Chad Gorshing

Maybe Groovy doesn't have 'native' support for zip files, but it is still pretty trivial to work with them.

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

I'm working with zip files and the following is some of the logic I'm using:

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

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 findAllmethod:

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

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

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

回答by Kirk G

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

根据我的经验,最好的方法是使用 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.

这样你就不用负责做所有复杂的事情 - ant 会为你处理。显然,如果您需要更细粒度的东西,那么这将行不通,但对于大多数“只需解压缩此文件”的情况,这确实有效。

To use antbuilder, just include ant.jar and ant-launcher.jar in your classpath.

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

回答by Merlin

This article expands on the AntBuilder example.

本文扩展了 AntBuilder 示例。

http://preferisco.blogspot.com/2010/06/using-goovy-antbuilder-to-zip-unzip.html

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.

然而,作为一个原则 - 有没有办法找出在研究 groovy/java 中的新方面时可以使用的所有属性、闭包、映射等?似乎有很多真正有用的东西,但是如何解锁它们隐藏的宝藏呢?NetBeans/Eclipse 代码完整功能现在似乎在我们这里拥有的新语言丰富性中受到了无可救药的限制。

回答by Ganesh Krishnan

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()
}

回答by bugs_

Unzip using AntBuilder is good way.
Second option is use an third party library - I recommend Zip4j

使用 AntBuilder 解压缩是个好方法。
第二种选择是使用第三方库 - 我推荐Zip4j

回答by Andre Steingress

The Groovy common extension project provides this functionality for Groovy 2.0 and above: https://github.com/timyates/groovy-common-extensions

Groovy 通用扩展项目为 Groovy 2.0 及更高版本提供此功能:https: //github.com/timyates/groovy-common-extensions

回答by michael

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.

尽管将问题转向了另一个方向,但我开始将 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()
    }
}

Then, for example (this extracts the bar.zipand foo.tgzinto the directory build):

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

$ gradle doUnZip
$ gradle doUnTar

回答by Ravi Natesh

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

以下常规方法将解压缩到特定文件夹(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\'))
}