1 Reply Latest reply on Apr 16, 2009 7:37 AM by EJP

    Problem using ByteArrayOutputStream that's wrapped by ZIPOutputStream

      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);
                     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);
                     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"));
                     // CORRECT OUTPUT: ByteArrayOutputStream#writeTO() is correct.
                     FileOutputStream fout2 = new FileOutputStream(new File("C:/Documents and Settings/guest/Desktop/modified2.zip"));
                } catch (ZipException e) {
                } catch (IOException e) {
                } catch (Throwable e) {
      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.