title: How do I get the InputStream of decompressed data from an InputStream of GZIPed data?
body: <p>I call a service which returns a gzipped file. I have the data as an InputStream (courtesy of <code>javax.activation.DataHandler.getInputStream();</code>) from the response. </p>
  
  <p>What I would like to do is, without writing anything to disk, get an InputStream of the decompressed data in the file that is in the archive. The compressed file in this case is an xml document that I am trying to unmarshal using <code>javax.xml.bind.Unmarshaller</code> which takes an InputStream. </p>
  
  <p>I'm currently trying to write the InputStream to an OutputStream (decompressing the data) and then I'll need to write it back to an InputStream. It's not working yet so I thought I would see if there was a better (I would hope so) approach.</p>
  
  <p>I can write the initial InputStream to disk and get a gz file, and then read that file, get the compressed file out of it and go from there but I'd rather keep it all in memory is possible.</p>
  
  <p>Update 1: Here is my current (not working - get a "Not in GZIP format" exception):</p>
  
  <pre><code>    ByteArrayInputStream xmlInput = null;
      try {
          InputStream in = dh.getInputStream(); //dh is a javax.activation.DataHandler
          BufferedInputStream bis = new BufferedInputStream(in);
          ByteArrayOutputStream bo = new ByteArrayOutputStream();
          int bytes_read = 0;
          byte[] dataBuf = new byte[4096];
          while ((bytes_read = bis.read(dataBuf)) != -1) {
              bo.write(dataBuf, 0, bytes_read);
          }
          ByteArrayInputStream bin = new ByteArrayInputStream(bo.toByteArray());
          GZIPInputStream gzipInput = new GZIPInputStream(bin);
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          dataBuf = new byte[4096];;
          bytes_read = 0;
          while ((bytes_read = gzipInput.read(dataBuf)) &gt; 0) {
              out.write(dataBuf, 0, bytes_read);
          }
          xmlInput = new ByteArrayInputStream(out.toByteArray());
  </code></pre>
  
  <p>If instead of writing to a ByteArrayOutputStream I write to a FileOutputStream the first time around I get a compressed file (which I can manually open to get the xml file within) and the service (eBay) says it should be a gzip file so I'm not sure why I get a "Not in GZIP format" error.</p>
  
  <p>Update 2: I tried something a little different - same error ("Not in GZIP format"). Wow, I just tried to end that parenthesis with a semi-colon. Anyways, here is my second attempt, which still does not work:</p>
  
  <pre><code>    ByteArrayInputStream xmlInput = null;
      try {
          GZIPInputStream gzipInput = new GZIPInputStream(dh.getInputStream());
          ByteArrayOutputStream bo = new ByteArrayOutputStream();
          int bytes_read = 0;
          byte[] dataBuf = new byte[4096];
          while ((bytes_read = gzipInput.read(dataBuf)) != -1) {
              bo.write(dataBuf, 0, bytes_read);
          }
          xmlInput = new ByteArrayInputStream(bo.toByteArray());
  </code></pre>