1 Reply Latest reply: Apr 16, 2009 2:37 AM by EJP RSS

    Problem using ByteArrayOutputStream that's wrapped by ZIPOutputStream

    807588
      Hi, I'm using a ByteArrayOutputStream that's wrapped by ZIPOutputStream.
      Scenario 1:*
      I retrieved the byte array from ByteArrayOutputStream#toByteArray()and when I try to write the byte[] to FileOutPutStream the output file is not a valid archive.
      Scenario 2:*
      I used ByteArrayOutputStream#writeTO to which generated the output valid output.
      package com.vst.test;
      
      import java.io.ByteArrayOutputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.util.zip.Deflater;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipException;
      import java.util.zip.ZipInputStream;
      import java.util.zip.ZipOutputStream;
      
      public class Test {
      
           public static void main(String[] args) {
                byte[] result = null;
                try {
                     FileInputStream fis = new FileInputStream(new File("C:/Documents and Settings/guest/Desktop/test.zip"));
                     ZipInputStream zis = new ZipInputStream(fis);
      
                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
                     ZipOutputStream out = new ZipOutputStream(bos);
                     out.setLevel(Deflater.DEFAULT_COMPRESSION);
      
                     ZipEntry entry;
                     while ((entry = zis.getNextEntry()) != null) {
                          out.putNextEntry(new ZipEntry(entry.getName()));
                          int len;
                          byte[] byt = new byte[2048];
                          while ((len = zis.read(byt)) > 0) {
                               out.write(byt, 0, len);
                          }
                          out.closeEntry();
                     }
      
                     result = bos.toByteArray();
                     // INCORRECT OUTPUT: ByteArrayOutputStream#toByteArray() to FileOutputStream is incorrect.
                     FileOutputStream fout1 = new FileOutputStream(new File("C:/Documents and Settings/guest/Desktop/modified1.zip"));
                     fout1.write(result);
      
                     // CORRECT OUTPUT: ByteArrayOutputStream#writeTO() is correct.
                     FileOutputStream fout2 = new FileOutputStream(new File("C:/Documents and Settings/guest/Desktop/modified2.zip"));
                     bos.writeTo(fout2);
      
                     fis.close();
                     bos.close();
                     fout1.close();
                     fout2.close();
                     bos.close();
                     out.close();
                     zis.close();
      
                } catch (ZipException e) {
                     e.printStackTrace();
                } catch (IOException e) {
                     e.printStackTrace();
                } catch (Throwable e) {
                     e.printStackTrace();
                }
      
           }
      }
      In a real time scenario i will be saving the byte array to a DataBase and will convert back to zip file when needed.

      Any suggestions or comments?

      Should i post a bug?

      Thanks in advance.