Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8295864

Zip file can not be opened because of ZipOutputStream bug

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      windows 11, Java 8

      A DESCRIPTION OF THE PROBLEM :
      ZipOutputStream must call close() before ByteArrayOutputStream calls toByteArray() to obtain the whole byte[]

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.io.IOException;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;

      public class Main {

          @Test
          public void testZipNoFile() throws IOException {
              ByteArrayOutputStream fos = new ByteArrayOutputStream();
              ZipOutputStream zipOut = new ZipOutputStream(fos);
              ZipEntry zipEntry = new ZipEntry("text1.txt");
              zipOut.putNextEntry(zipEntry);
              zipOut.write("你好世界".getBytes("utf-8"));
              //zipOut.close();
              byte[] zipByte = fos.toByteArray();
              fos.close();
              zipOut.close();
              FileOutputStream fouts = new FileOutputStream("/c:/90087.zip");
              fouts.write(zipByte);
              fouts.close();
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      zip file created correctly
      ACTUAL -
      can not open zip file

      CUSTOMER SUBMITTED WORKAROUND :


      FREQUENCY : always

        1. Main.java
          0.8 kB
        2. ERROR.jpg
          ERROR.jpg
          27 kB

            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: