为什么GZip压缩算法的结果是不是在安卓和.Net一样吗? [英] Why the result of GZip algorithm is not same in Android and .Net?
问题描述
为什么GZip压缩算法的结果是不一样的安卓和.Net?
Why the result of GZip algorithm is not same in Android and .Net?
我的code在机器人:
My code in android:
public static String compressString(String str) {
String str1 = null;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
BufferedOutputStream dest = null;
byte b[] = str.getBytes();
GZIPOutputStream gz = new GZIPOutputStream(bos, b.length);
gz.write(b, 0, b.length);
bos.close();
gz.close();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
byte b1[] = bos.toByteArray();
return Base64.encode(b1);
}
我的code。在.NET Web服务上:
My code in the .Net WebService:
public static string compressString(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}
在Android的:
compressString("hello"); -> "H4sIAAAAAAAAAMtIzcnJBwCGphA2BQAAAA=="
在净:
compressString("hello"); -> "BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA=="
有趣的是,当我使用 DECOM preSS 方式在Android中,以DECOM preSS净结果 COM pressString 方式,它正确返回原始字符串,但我得到的错误,当我DECOM preSS的结果机器人 COM pressedString 方式。
It is interesting that when I use Decompress method in android to decompress the result of .Net compressString method, it returns the original string correctly but I get error when I decompress the result of android compressedString method.
Android的DECOM preSS方式:
Android Decompress method:
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");
}
我认为有Android中的 COM pressString 方式错误。 任何人可以帮助我吗?
I think that there is an error in Android compressString method. Can anybody help me?
推荐答案
根据这个答案,我有4种方法。 Android和.NET COM preSS和DECOM preSS方法。这些方法是彼此相容除了一种情况
According to this answer, I have 4 methods. Android and .net compress and decompress methods. These methods are compatible with each other except in one case.
这篇关于为什么GZip压缩算法的结果是不是在安卓和.Net一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!