compress string and return the compressed a string - Android java.util.zip

Android examples for java.util.zip:Zip

Description

compress string and return the compressed a string

Demo Code

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class Main {

  public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
      return str;
    }/*w  w w. ja  va 2s.c om*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toString("ISO-8859-1");
  }

}

Related Tutorials