title: How to check if InputStream is Gzipped?
body: <p>Is there any way to check if InputStream has been gzipped?
  Here's the code:</p>
  
  <pre><code>public static InputStream decompressStream(InputStream input) {
      try {
          GZIPInputStream gs = new GZIPInputStream(input);
          return gs;
      } catch (IOException e) {
          logger.info("Input stream not in the GZIP format, using standard format");
          return input;
      }
  }
  </code></pre>
  
  <p>I tried this way but it doesn't work as expected - values read from the stream are invalid.
  EDIT:
  Added the method I use to compress data: </p>
  
  <pre><code>public static byte[] compress(byte[] content) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try {
          GZIPOutputStream gs = new GZIPOutputStream(baos);
          gs.write(content);
          gs.close();
      } catch (IOException e) {
          logger.error("Fatal error occured while compressing data");
          throw new RuntimeException(e);
      }
      double ratio = (1.0f * content.length / baos.size());
      if (ratio &gt; 1) {
          logger.info("Compression ratio equals " + ratio);
          return baos.toByteArray();
      }
      logger.info("Compression not needed");
      return content;
  
  }
  </code></pre>