0

我正在尝试解压缩包含其他 zip 文件的文件。我的尝试基于以下 Java2s 代码:http ://www.java2s.com/Code/Java/File-Input-Output/LoadresourcefromJarfile.htm 。

我唯一的困难是,当一个条目本身是一个 Jar 文件时,如果不先将其写入临时文件,我就无法解压缩它。

这是我正在做的事情:

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

然而,我真正想做的是:

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

我在第二个版本中看到的是,只有当我先将这个内部 jar 写入磁盘时,内部 jar 中包含的条目的大小信息才可用。

有没有办法避免这种情况?

非常感谢,克劳斯。

4

1 回答 1

0

我不知道你在用你的尺码做什么,我认为你让他们很困惑。ZipInputStream.getNextEntry() 将流定位在条目的开头。来自http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html#getNextEntry()

公共 ZipEntry getNextEntry() 抛出 IOException

读取下一个 ZIP 文件条目并将流定位在条目数据的开头。

所以,以下对我有用:

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));
        }
    }
}
于 2010-07-02T21:40:19.317 回答