title: How to compress a String in Java?
body: <p>I use <code>GZIPOutputStream</code> or <code>ZIPOutputStream</code> to compress a String (my <code>string.length()</code> is less than 20), but the compressed result is longer than the original string.</p>
  
  <p>On some site, I found some friends said that this is because my original string is too short, <code>GZIPOutputStream</code> can be used to compress longer strings.</p>
  
  <p>so, can somebody give me a help to compress a String?</p>
  
  <p>My function is like:</p>
  
  <pre><code>String compress(String original) throws Exception {
  
  }
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.util.zip.GZIPOutputStream;
  import java.util.zip.*;
  
  
  //ZipUtil 
  public class ZipUtil {
      public static String compress(String str) {
          if (str == null || str.length() == 0) {
              return str;
          }
  
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          GZIPOutputStream gzip = new GZIPOutputStream(out);
          gzip.write(str.getBytes());
          gzip.close();
          return out.toString("ISO-8859-1");
      }
  
      public static void main(String[] args) throws IOException {
          String string = "admin";
          System.out.println("after compress:");
          System.out.println(ZipUtil.compress(string));
      }
  }
  </code></pre>
  
  <p>The result is :</p>
  
  <p><img src="https://i.stack.imgur.com/NUYbH.jpg" alt="alt text"></p>