我想压缩我的字符串值的字符串。这些字符串值应与.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