[groovy-user] How to read from zip files in a zip file?

501 views
Skip to first unread message

citron

unread,
Jun 7, 2010, 7:50:19 AM6/7/10
to us...@groovy.codehaus.org

Hi,

Reading the entries from a zip file is straight forward

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

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

but how can I read the entries from a zip file in a zip file?


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

zipFile.entries().each {
if(it.name.contains(".zip")){
def zipInZipFile = new java.util.zip.ZipFile(new File(it)) //????
zipInZipFile.entries().each {
//????
println zipInZipFile.getInputStream(it).text //????
}
}
else{
it.name
}
}

Thanks for any ideas how to solve this!!
--
View this message in context: http://old.nabble.com/How-to-read-from-zip-files-in-a-zip-file--tp28804180p28804180.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Guillaume Laforge

unread,
Jun 7, 2010, 8:12:15 AM6/7/10
to us...@groovy.codehaus.org
Hello,

This is actually more a Java question than a Groovy question.
It seems ZipFile just takes a File as constructor parameter, if it'd
taken an InputStream, this would have been more straight-forward :-(
So apart from getting the input stream of those entries, storing it in
a temp folder, and going through it again with a ZipFile... I'm not
sure you can do much differently, at least not with what's available
in the JDK.
Anyhow, Groovy doesn't offer any particular support for that.

Guillaume

--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

Jason Smith

unread,
Jun 7, 2010, 11:25:59 AM6/7/10
to us...@groovy.codehaus.org
Guillaume is correct. This is more of a Java question.

You can use ZipInputStream, rather than ZipFile, to read a Zip file recursively. The limitation with ZipInputStream is that if you wish to read the content of the file, you must do so as you traverse the data. With ZipFile, you can access files in the archive in random order.

In other words, if you are accessing all the files sequentially and recursively, ZipInputStream is what you need. If you need to access things in arbitrary order, you'll have to extract the inner Zip files to temp files and use ZipFile.

Jason Smith
________________________________________
From: glaf...@gmail.com [glaf...@gmail.com] On Behalf Of Guillaume Laforge [glaf...@codehaus.org]
Sent: Monday, June 07, 2010 6:12 AM
To: us...@groovy.codehaus.org
Subject: Re: [groovy-user] How to read from zip files in a zip file?

Anders Viklund

unread,
Jun 7, 2010, 1:11:33 PM6/7/10
to us...@groovy.codehaus.org
This code could possible be beautified and modified to deal with recursive zip file entries, but it works good enough for me at the moment.
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.*;

public class PrintNestedZipFileNames {
  public static void main(String[] args) {
    ZipInputStream zis
    InputStream  is
    try {
      ZipFile zipFile = new ZipFile(new File("C:/workspace/someFile.zip"));
     
      for(Enumeration e = zipFile.entries();e.hasMoreElements();){
       ZipEntry ze = (ZipEntry)e.nextElement();
        if(ze.getName().contains(".zip")){
          is = zipFile.getInputStream(ze);
          zis = new ZipInputStream(is);
          ZipEntry zentry = zis.getNextEntry();
          while (zentry!=null){
              System.out.println(zentry.getName());
              zentry = zis.getNextEntry();
          }
          is.close();
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
        if(is!=null)
            is.close();
        if(zis!=null)
            zis.close();

Hotmail: Powerful Free email with security by Microsoft. Get it now.

Guillaume Laforge

unread,
Jun 7, 2010, 3:30:33 PM6/7/10
to us...@groovy.codehaus.org
Some groovification would be cool :-)
Reply all
Reply to author
Forward
0 new messages