title: compression and decompression of string data in java
body: <p>I am using the following code to compress and decompress string data, but the problem which I am facing is, it is easily getting compressed without error, but the decompress method throws the following error.</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; java.io.IOException: Not in GZIP format</p>
  </blockquote>
  <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) throws Exception {
      String string = &quot;I am what I am hhhhhhhhhhhhhhhhhhhhhhhhhhhhh&quot;
              + &quot;bjggujhhhhhhhhh&quot;
              + &quot;rggggggggggggggggggggggggg&quot;
              + &quot;esfffffffffffffffffffffffffffffff&quot;
              + &quot;esffffffffffffffffffffffffffffffff&quot;
              + &quot;esfekfgy enter code here`etd`enter code here wdd&quot;
              + &quot;heljwidgutwdbwdq8d&quot;
              + &quot;skdfgysrdsdnjsvfyekbdsgcu&quot;
              + &quot;jbujsbjvugsduddbdj&quot;;
  
      System.out.println(&quot;after compress:&quot;);
      String compressed = compress(string);
      System.out.println(compressed);
      System.out.println(&quot;after decompress:&quot;);
      String decomp = decompress(compressed);
      System.out.println(decomp);
  }
  
  
  public static String compress(String str) throws Exception {
      if (str == null || str.length() == 0) {
          return str;
      }
      System.out.println(&quot;String length : &quot; + str.length());
      ByteArrayOutputStream obj=new ByteArrayOutputStream();
      GZIPOutputStream gzip = new GZIPOutputStream(obj);
      gzip.write(str.getBytes(&quot;UTF-8&quot;));
      gzip.close();
      String outStr = obj.toString(&quot;UTF-8&quot;);
      System.out.println(&quot;Output String length : &quot; + outStr.length());
      return outStr;
  }
  
  public static String decompress(String str) throws Exception {
      if (str == null || str.length() == 0) {
          return str;
      }
      System.out.println(&quot;Input String length : &quot; + str.length());
      GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes(&quot;UTF-8&quot;)));
      BufferedReader bf = new BufferedReader(new InputStreamReader(gis, &quot;UTF-8&quot;));
      String outStr = &quot;&quot;;
      String line;
      while ((line=bf.readLine())!=null) {
          outStr += line;
      }
      System.out.println(&quot;Output String lenght : &quot; + outStr.length());
      return outStr;
  }
  </code></pre>
  <p>Still couldn't figure out how to fix this issue!</p>