I use GZIPOutputStream or ZIPOutputStream to compress a String( my string.length() is less than 20), but the compress result is longer than original string.
on some site, I found some friends said this is because my original string is too short, GZIPOutputStream is only do use to compress long string.
so, can somebody give me a help to compress a String? the function is like:
String compress(String original) throws Exception {
}
Update:
import java.io.ByteArrayOutputStream;
java.io.IOException;
import java.util.zip.GZIPOutputStream;
import java.util.zip.*;
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));
}
}
the result is :