简体   繁体   English

Java ZipInputStream无法读取整个ZipEntry

[英]Java ZipInputStream not reading entire ZipEntry

I'm trying to read an XML file out of a ZIP archive. 我正在尝试从ZIP存档中读取XML文件。 The relevant code is below: 相关代码如下:

ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
    if(entry.getName().equals("plugin.xml")) {
        int size = (int)entry.getSize();
        byte[] bytes = new byte[size];
        int read = zis.read(bytes, 0, size);

        System.out.println("File size: " + size);
        System.out.println("Bytes read: " + read);
    }
}

This, when working produces output as follows: 这在工作时产生如下输出:

File size: 5224
Bytes read: 5224

The plugin.xml file being read is nothing special, and passes any XML validation I can find, however, minor changes to the XML file (deleting characters, adding characters, etc.) sometimes causes a situation where the "bytes read" from the input stream is less than the file size. 正在读取的plugin.xml文件没有什么特别的,并且可以通过我能找到的任何XML验证,但是,对XML文件进行的细微更改(删除字符,添加字符等) 有时会导致从文件中读取“字节”的情况。输入流小于文件大小。 In this case, I changed the text value of an XML attribute of the same file as above and got the following result: 在这种情况下,我更改了与上述相同文件的XML属性的文本值,并得到以下结果:

File size: 5218
Bytes read: 5205 // the reader stopped early!

I cannot see any pattern in terms of which XML files will work and which won't. 我看不到任何模式可以使用哪些XML文件,哪些不能使用。 It seems to be completely random. 这似乎是完全随机的。

Has anyone come across anything like this before? 有人遇到过这样的事情吗?

Edit: Forgot to mention, the Java code which reads in the plugin.xml file is embedded in an off-the-shelf application which I cannot change. 编辑:忘了提及,在plugin.xml文件中读取的Java代码嵌入在我无法更改的现成应用程序中。 My issue is trying to understand why it won't accept my XML file in some cases. 我的问题是试图了解为什么在某些情况下它不接受我的XML文件。

Where does it say that InputStream.read() , or any of its implementations or overrides, fills the buffer? 它在哪里说InputStream.read()或其任何实现或重写填充缓冲区? Check the Javadoc. 检查Javadoc。 What is actually says is that read() either returns -1 indicating EOS or reads at least one byte into the buffer. 实际上,是说read()返回-1表示EOS或将至少一个字节读入缓冲区。 You have to loop. 你必须循环。

As was mentioned before, you need to use a loop. 如前所述,您需要使用循环。 I had to solve this exact problem, so I figured I would post an example. 我必须解决这个确切的问题,所以我想举个例子。

ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
    if(entry.getName().equals("plugin.xml")) {
        int size = (int)entry.getSize();
        byte[] bytes = new byte[size];
        int read = 0;
        while (read < size) {
            read += zis.read(bytes, read, (size - read));
        }

        System.out.println("File size: " + size);
        System.out.println("Bytes read: " + read);
    }
}

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

相关问题 关于从Java中的ZipInputStream / ZipEntry读取的问题 - Question about reading from a ZipInputStream/ZipEntry in java ZipInputStream 在读取 Excel 文件的第一个 ZipEntry 时关闭 - ZipInputStream is closes when reading first ZipEntry of Excel files Java ZipInputStream 跳过未使用的 ZipEntry 内容,而不是耗尽它 - Java ZipInputStream skipping unused ZipEntry content, rather than draining it 从ZipInputStream Java读取时的IndexOutOfBoundsException - IndexOutOfBoundsException when reading from a ZipInputStream Java Android ZipInputStream ZipEntry不读取到文件末尾 - Android ZipInputStream ZipEntry does not read to end of file 是否有可能从zipinputstream获得zipentry的输入流? - is it possible to get a zipentry's inputstream from a zipinputstream? ZipInputStream和JarInputStream的ZipEntry大小之间的不一致 - Inconsistency between ZipEntry size for ZipInputStream and JarInputStream 使用Java SE6时读取包含特殊字符的ZipEntry - Reading ZipEntry containing special characters while using Java SE6 从ZipInputStream读取到ByteArrayOutputStream - Reading from a ZipInputStream into a ByteArrayOutputStream 来自ZipInputStream的ZipEntry的getInputStream(不使用ZipFile类) - getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM