简体   繁体   English

从流中读取JarEntry

[英]Reading JarEntry from stream

I have jar as input stream. 我有jar作为输入流。 I want to read JarEntry directly from stream. 我想直接从流中读取JarEntry。 I do not have jar file, and I don't want to create temporar jar file. 我没有jar文件,也不想创建临时的jar文件。

I tried code like this: 我试过这样的代码:

    JarInputStream jis = new JarInputStream(in);
    JarEntry je = null;

    while ((je = jis.getNextJarEntry()) != null)
    {
        byte[] classbytes = new byte[(int) je.getSize()];
        System.out.println("size of array: " + classbytes.length);
        jis.read(classbytes, 0, classbytes.length);
    }

The problem with this code: je.getSize() returns -1 (it means jarEntry size unknown). 此代码的问题:je.getSize()返回-1(这意味着jarEntry大小未知)。

1) Maybe you have idea how to work around this? 1)也许您知道如何解决此问题?

2) Maybe you know why jarEntry size is unknown? 2)也许您知道为什么jarEntry大小未知? (I used Eclipse File>Export>Jar File to create it) (我使用Eclipse File> Export> Jar File来创建它)

You can loop around the read method and read into a fixed sized array, appending the just read bytes into the final storage. 您可以遍历read方法并读入固定大小的数组,将刚刚读取的字节附加到最终存储中。

Alternatively you can use a different type inputStream as passed into your JarInputStream. 另外,您可以使用其他类型的inputStream传递到JarInputStream中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM