I use GZIPOutputStream or ZIPOutputStream to compress the string (my string.length() less than 20), but the compressed result is longer than the original string.
On some site, I found that some friends said that it was because my source line was too short, GZIPOutputStream could be used to compress longer lines.
so can anyone help me squeeze the string?
My function is similar:
String compress(String original) throws Exception { }
Update:
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)); } }
Result:

java string compression zip
user421851 Sep 06 '10 at 6:40 2010-09-06 06:40
source share