Reading from a ZipInputStream into a ByteArrayOutputStream

后端 未结 10 1518
旧时难觅i
旧时难觅i 2021-02-05 08:14

I am trying to read a single file from a java.util.zip.ZipInputStream, and copy it into a java.io.ByteArrayOutputStream (so that I can then create a

10条回答
  •  抹茶落季
    2021-02-05 08:26

    You're missing call

    ZipEntry entry = (ZipEntry) zipStream.getNextEntry();

    to position the first byte decompressed of the first entry.

     ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
     int bytesRead;
     byte[] tempBuffer = new byte[8192*2];
     ZipEntry entry = (ZipEntry) zipStream.getNextEntry();
     try {
         while ( (bytesRead = zipStream.read(tempBuffer)) != -1 ){
            streamBuilder.write(tempBuffer, 0, bytesRead);
         }
     } catch (IOException e) {
          ...
     }
    

提交回复
热议问题