title: Java: how to compress a byte[] using ZipOutputStream without intermediate file
body: <p>Requirement: compress a byte[] to get another byte[] using java.util.zip.ZipOutputStream BUT without using any files on disk or in-memory(like here <a href="https://stackoverflow.com/a/18406927/9132186">https://stackoverflow.com/a/18406927/9132186</a>). Is this even possible?</p>
  
  <p>All the examples I found online read from a file(.txt) and write to a file(.zip). ZipOutputStream needs a ZipEntry to work with and that ZipEntry needs a file. </p>
  
  <p>However, my use case is as follows: I need to compress a chunk (say 10MB) of a file at a time using a zip format and append all these compressed chunks to make a .zip file. But, when I unzip the .zip file then it is corrupted.</p>
  
  <p>I am using in-memory files as suggested in <a href="https://stackoverflow.com/a/18406927/9132186">https://stackoverflow.com/a/18406927/9132186</a> to avoid files on disk but need a solution without these files also.</p>
  
  <pre><code>  public void testZipBytes() {
      String infile = "test.txt";
      FileInputStream in = new FileInputStream(infile);
  
      String outfile = "test.txt.zip";
      FileOutputStream out = new FileOutputStream(outfile);
  
      byte[] buf = new byte[10];
      int len;
      while ((len = in.read(buf)) &gt; 0) {
        out.write(zipBytes(buf));
      }
      in.close();
      out.close();
    }
  
    // ACTUAL function that compresses byte[]
  
    public static class MemoryFile {
      public String fileName;
      public byte[] contents;
    }
  
    public byte[] zipBytesMemoryFileWORKS(byte[] input) {
      MemoryFile memoryFile = new MemoryFile();
      memoryFile.fileName = "try.txt";
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ZipOutputStream zos = new ZipOutputStream(baos);
      ZipEntry entry = new ZipEntry(memoryFile.fileName);
      entry.setSize(input.length); 
      zos.putNextEntry(entry); 
      zos.write(input);
      zos.finish();
      zos.closeEntry();
      zos.close();
      return baos.toByteArray();
    }
  </code></pre>
  
  <p>Scenario 1:
  if test.txt has small amount of data (less than 10 bytes) like "this" then <code>unzip test.txt.zip</code> yeilds <code>try.txt</code> with "this" in it.</p>
  
  <p>Scenario 2:
  if test.txt has larger amount of data (more than 10 bytes) like "this is a test for zip output stream and it is not working" then <code>unzip test.txt.zip</code> yields <code>try.txt</code> with broken pieces of data and is incomplete.</p>
  
  <p>this 10 bytes is the buffer size in testZipBytes and is the amount of data that is compressed at a time by zipBytes</p>
  
  <p>Expected (or rather desired):
  1. <code>unzip test.txt.zip</code> does not use the "try.txt" filename i gave in the MemoryFile but rather unzips to filename test.txt itself.
  2. unzipped data is not broken and yields the input data as is.
  3. I have done the same with GzipOutputStream and it works perfectly fine.</p>