title: Write ZipEntry with given byte array in memory
body: <p>I have a very confusing problem and hope that I can get some ideas here.</p>
  
  <p>My problem is very simple, but I didn't find a solution yet.</p>
  
  <p>I want to create a simple ZIP File with ZipEntry's in it. The ZipEntry's are created by a given byte array (saved in a Postgres-DB with Hibernate). </p>
  
  <p>When I put this byte array into my ZipOutputStream.write(..) the ZIP File created is always corrupt. What am I doing wrong? </p>
  
  <p>The ZIP File is transferred to a FTP-Server afterwards.</p>
  
  <pre><code>ByteArrayOutputStream bos = new ByteArrayOutputStream();
  final ZipOutputStream zipOut = new ZipOutputStream(bos);
  String filename = "test.zip";
  
  for(final Attachment attachment : transportDoc.getAttachments()) {
      log.debug("Adding "+attachment.getFileName()+" to ZIP file /tmp/"+filename);
  
      ZipEntry ze = new ZipEntry(attachment.getFileName());
      zipOut.putNextEntry(ze);
      zipOut.write(attachment.getFileContent());
      zipOut.flush();
      zipOut.closeEntry();
  }
  
  zipOut.close();
  org.apache.commons.io.FileUtils.writeByteArrayToFile(new File("/tmp/"+filename), bos.toByteArray());
  </code></pre>
  
  <p>I am confused, because when I replaced</p>
  
  <pre><code>zipOut.write(attachment.getFileContent()); //This is the byte array from db
  </code></pre>
  
  <p>with</p>
  
  <pre><code>zipOut.write("Bla bla".getBytes());
  </code></pre>
  
  <p>it worked! </p>
  
  <p>But the byte array from the DB can't be corrupt, because it can be written to a file with</p>
  
  <pre><code>org.apache.commons.io.FileUtils.writeByteArrayToFile(new File("/tmp/test.png"), attachment.getFileContent());
  </code></pre>
  
  <p>with no problem. It is a correct file.</p>
  
  <p>I hope you have some ideas left.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tried to repair the ZIP file offline and then this messages appears:</p>
  
  <pre><code>zip warning: no end of stream entry found: cglhnngplpmhipfg.png
  </code></pre>
  
  <p>(This png file is the byte-Array-File)</p>
  
  <p>Simple unzip-command output the following:</p>
  
  <pre><code>unzip created.zip 
  Archive:  created.zip
  error [created.zip]:  missing 2 bytes in zipfile
    (attempting to process anyway)
  error [created.zip]:  attempt to seek before beginning of zipfile
    (please check that you have transferred or created the zipfile in the
  appropriate BINARY mode and that you have compiled UnZip properly)
    (attempting to re-compensate)
  replace cglhnngplpmhipfg.png? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
    inflating: cglhnngplpmhipfg.png    
    error:  invalid compressed data to inflate
  file #2:  bad zipfile offset (local header sig):  24709
    (attempting to re-compensate)
    inflating: created.xml
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>When I write this file to the Filesystem and add this file to the ZIP by an InputStream it doesn't work either! But the File on the Filesystem is ok. I can open the Image with no problem. Its very confusing</p>
  
  <pre><code>File tmpAttachment = new File("/tmp/"+filename+attachment.getFileName());
  FileUtils.writeByteArrayToFile(tmpAttachment, attachment.getFileContent());
  FileInputStream inTmp = new FileInputStream(tmpAttachment);
  int len;
  byte[] buffer = new byte[1024];
  while ((len = inTmp.read(buffer)) &gt; 0) {
       zipOut.write(buffer, 0, len);
  }
  inTmp.close();
  </code></pre>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>This problem only appears when I try to add "complex" files like png or pdf. If I put a txt-file in it, it works.</p>