Why ZipInputStream can't read the output of ZipOutputStream?

后端 未结 5 1985
夕颜
夕颜 2021-01-18 08:20

I\'m stuck with this junit test:

public void test() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipOutputStream zipOu         


        
5条回答
  •  半阙折子戏
    2021-01-18 08:46

    Recently I had a similar problem while reading a zip bytes created using ZipOutputStream.

    The following snippet led to java.lang.NegativeArraySizeException.

    zipEntry = zipInputStream.getNextEntry();
    byte[] bytes = new byte[(int) zipEntry.getSize()];
    

    An quick solution it was to use Apache commons-io IOUtils to read the all bytes of current entry.

    zipEntry = zipInputStream.getNextEntry();
    byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(zipInputStream);
    

    I hope this will be helpful to someone.

提交回复
热议问题