tags:

views:

2117

answers:

4

Hi, is there a built-in support in Groovy to handle Zip files (the groovy way)? Or do i have to use java.util.zip.ZipFile to process Zip files in Groovy ?

+2  A: 

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

John Feminella
+3  A: 

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
}
Chad Gorshing
+3  A: 

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.

Kirk G
A: 

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.

Merlin