为什么GZip算法在Android和.Net中的结果不一样?
我在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);
}
我在 .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);
}
在安卓中:
compressString("hello"); -> "H4sIAAAAAAAAAMtIzcnJBwCGphA2BQAAAA=="
在 .Net 中:
compressString("hello"); -> "BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA=="
有趣的是,当我在 android 中使用 Decompress 方法解压缩 .Net compressString 方法的结果时,它正确地返回了原始字符串,但是当我解压缩 android compressedString 方法的结果。
安卓解压方法:
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 compressString 方法有错误。 谁能帮帮我?
最佳答案
在 Android 版本中,您应该在关闭 gz
之后关闭 bos
。
此外,compressString
中的这一行可能会给您带来问题:
byte b[] = str.getBytes();
这将使用设备上的默认编码将字符转换为字节,这几乎肯定不是 UTF-8。另一方面,.NET 版本使用的是 UTF8。在 Android 中,试试这个:
byte b[] = str.getBytes("UTF-8");
编辑:在进一步查看您的代码时,我建议您像这样重写它:
byte b[] = str.getBytes("UTF-8");
GZIPOutputStream gz = new GZIPOutputStream(bos);
gz.write(b, 0, b.length);
gz.finish();
gz.close();
bos.close();
变化是:使用UTF-8编码字符;使用 GZIPOutputStream 的默认内部缓冲区大小;在调用 bos.close()
之前调用 gz.close()
(后者可能甚至不需要);并在调用 gz.close()
之前调用 gz.finish()
。
编辑 2:
好吧,我早该意识到发生了什么。在我看来,GZIPOutputStream 类是一个愚蠢的设计。它无法定义您想要的压缩,默认压缩设置为无。您需要对其进行子类化并覆盖默认压缩。最简单的方法是这样做:
GZIPOutputStream gz = new GZIPOutputStream(bos) {
{
def.setLevel(Deflater.BEST_COMPRESSION);
}
};
这将重置 GZIP 用于提供最佳压缩的内部压缩器。 (顺便说一下,如果您不熟悉它,我在这里使用的语法称为 instance initializer block。)
关于android - 为什么 GZip 算法的结果在 Android 和 .Net 中不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055019/