我想壓縮我的字符串值的字符串。這些字符串值應與.net壓縮字符串相同。壓縮使用GZIPOutputStream
我寫了解壓縮方法和當我發送一個.net壓縮字符串,它工作正常。但壓縮方法無法正常工作。
public static String Decompress(String zipText) throws IOException {
int size = 0;
byte[] gzipBuff = Base64.decode(zipText);
ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 4,
gzipBuff.length - 4);
GZIPInputStream gzin = new GZIPInputStream(memstream);
final int buffSize = 8192;
byte[] tempBuffer = new byte[buffSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
baos.write(tempBuffer, 0, size);
}
byte[] buffer = baos.toByteArray();
baos.close();
return new String(buffer, "UTF-8");
}
-
public static String Compress(String text) throws IOException {
byte[] gzipBuff = EncodingUtils.getBytes(text, "UTF-8");
ByteArrayOutputStream bs = new ByteArrayOutputStream();
GZIPOutputStream gzin = new GZIPOutputStream(bs);
gzin.write(gzipBuff);
gzin.finish();
bs.close();
byte[] buffer = bs.toByteArray();
gzin.close();
return Base64.encode(buffer);
}
例如當我發送「BQAAAB + LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee ++ 997o7nU4n99 // P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/W + GphA2BQAAAA ==」到解壓方法它返回字符串「你好」,但是當我送「你好」到壓縮方法,它返回「H4sIAAAAAAAAAMtIzcnJBwCGphA2BQAAAA ==」。
什麼是壓縮方法的問題????
在機器人抱歉構造函數字符串(字符串,字符串)是未定義 – breceivemail
文檔說說encodeToString()方法:[Base64.html](http://developer.android.com/reference/android/util /Base64.html#encode%28byte[],%20int%29)。我無法嘗試,我無法在我的電腦上安裝android。 – revo