views:

69

answers:

1

Hi Folks,

I'm trying to unzip a files that contain other zip files. My attempt is based on the following Java2s code: http://www.java2s.com/Code/Java/File-Input-Output/LoadresourcefromJarfile.htm.

My only difficulty is that when an entry is itself a Jar file, I'm not able to unzip it without writing it to a temp file first.

Here is what I'm doing:

 public void load(String jarFileName) throws Exception{
  ZipFile zf = new ZipFile(jarFileName);
  load(zf.entries(),new FileInputStream(jarFileName),zf.size());
 }
 public void load(Enumeration<? extends ZipEntry> enumeration, InputStream             inputStream, int total) throws Exception {
  if(enumeration!=null){
   while (enumeration.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) enumeration.nextElement();
    htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
   }
  }

  BufferedInputStream bis = new BufferedInputStream(inputStream);
  ZipInputStream zis = new ZipInputStream(bis);
  ZipEntry ze = null;
  int retrieved=0;
  while ((ze = zis.getNextEntry()) != null) {
   if (ze.isDirectory()) {
    continue;
   }
   int size = (int) ze.getSize();
   if (size == -1 && htSizes.get(ze.getName())!=null) {
    size = ((Integer) htSizes.get(ze.getName())).intValue();
   }
   if(size==-1)
    size=total-retrieved;
   retrieved+=size;
   byte[] b = new byte[(int) size];
   int rb = 0;
   int chunk = 0;
   while (((int) size - rb) > 0) {
    chunk = zis.read(b, rb, (int) size - rb);
    if (chunk == -1) {
     break;
    }
    rb += chunk;
   }
   if(ze.getName().endsWith(".jar")){
    File f=File.createTempFile("temp", System.nanoTime()+"");
    f.deleteOnExit();
    FileOutputStream fos= new FileOutputStream(f);
    fos.write(b);
    fos.close();
    load(f.getAbsolutePath());
   }

   else htJarContents.put(ze.getName(), b);
  }
 }

What I really want to do, however is this:

 public void load(String jarFileName) throws Exception{
  ZipFile zf = new ZipFile(jarFileName);
  load(zf.entries(),new FileInputStream(jarFileName),zf.size());
 }
 public void load(Enumeration<? extends ZipEntry> enumeration, InputStream inputStream, int total) throws Exception {
  if(enumeration!=null){
   while (enumeration.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) enumeration.nextElement();
    htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
   }
  }

  BufferedInputStream bis = new BufferedInputStream(inputStream);
  ZipInputStream zis = new ZipInputStream(bis);
  ZipEntry ze = null;
  int retrieved=0;
  while ((ze = zis.getNextEntry()) != null) {
   if (ze.isDirectory()) {
    continue;
   }
   int size = (int) ze.getSize();
   if (size == -1 && htSizes.get(ze.getName())!=null) {
    size = ((Integer) htSizes.get(ze.getName())).intValue();
   }
   if(size==-1)
    size=total-retrieved;
   retrieved+=size;
   byte[] b = new byte[(int) size];
   int rb = 0;
   int chunk = 0;
   while (((int) size - rb) > 0) {
    chunk = zis.read(b, rb, (int) size - rb);
    if (chunk == -1) {
     break;
    }
    rb += chunk;
   }
   if(ze.getName().endsWith(".jar")){
    load(null,new ByteArrayOutputstream(b));
   }   
   else htJarContents.put(ze.getName(), b);
  }
 }

What I'm seeing wit the second version is that the size information of the entries contained in the inner-jars are available only if I write this inner jar to a the disk first.

is there a way to avoid this?

Thanks so much, Klaus.

A: 

I don't know exactly what you're doing with your sizes, I think you're getting them really confused. ZipInputStream.getNextEntry() positions the stream at the beginning of the entry. From http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html#getNextEntry()

public ZipEntry getNextEntry() throws IOException

Reads the next ZIP file entry and positions stream at the beginning of the entry data.

So, the following works for me:

public void process(String filename) throws IOException {
    process(filename, new FileInputStream(new File(filename)));
}

private void process(String filename, InputStream inputStream) throws IOException {
    ZipInputStream zis = new ZipInputStream(inputStream);
    ZipEntry ze = null;

    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        System.out.println(ze.getName() + " " + ze.getSize());

        if (ze.getName().endsWith(".jar")) {
            long size = ze.getSize();

            byte[] b = new byte[(int) size];

            int rb = 0;
            int chunk = 0;
            while (((int) size - rb) > 0) {
                chunk = zis.read(b, rb, (int) size - rb);
                if (chunk == -1) {
                    break;
                }
                rb += chunk;
            }

            process(ze.getName(), new ByteArrayInputStream(b));
        }
    }
}
MatthieuF
Thanks you Matt. It seems to work. Not sure why I was getting getSize() returning -1.
Klaus