Unzip Archive with Groovy
Asked Answered
R

9

31

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 ?

Ride answered 14/3, 2009 at 12:28 Comment(1)
Is this still a valid question/answer? What Groovy version is this referring too?Intramundane
H
6

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.

Homocercal answered 14/3, 2009 at 12:32 Comment(1)
Unfortunately, that article disappeared from the blog. Luckily there is an archived version. But the More Groovy Power article is also good.Gar
B
54

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
}
Bracci answered 15/8, 2009 at 2:26 Comment(1)
+1 for reminding me that groovy's .text replaces all the traditional java stream handling boiler plate.Pacifism
O
40

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.

Obtuse answered 10/2, 2010 at 16:31 Comment(4)
@{Kirk G}. Do you know if following this approach, it is possible to make AntBuider quiet (not outputting to screen)?Federalese
how do you use patterns here? how to unzip selective files?Enwomb
Note that ant.unzip is a simple wrapper around the existing ant unzip task. The parameters you give are simply passed directly. See ant.apache.org/manual/Tasks/unzip.html for more information.Durman
This approach is massively slowFishman
H
6

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.

Homocercal answered 14/3, 2009 at 12:32 Comment(1)
Unfortunately, that article disappeared from the blog. Luckily there is an archived version. But the More Groovy Power article is also good.Gar
M
4

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

Malti answered 25/4, 2013 at 14:28 Comment(0)
S
4

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\\'))
}
Slick answered 16/11, 2016 at 5:41 Comment(0)
U
1

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.

Unready answered 23/6, 2010 at 10:8 Comment(0)
C
0

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

Companionate answered 18/6, 2012 at 10:46 Comment(0)
C
0

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
Cowitch answered 27/1, 2014 at 18:10 Comment(0)
C
-2
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()
}
Clippers answered 25/8, 2011 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.