Java和Go的gzip使用_为什么Java和Go的gzip会得到不同的结果?

首先,我的Java版本:

string str = "helloworld";

ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(str.length());

GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);

localGZIPOutputStream.write(str.getBytes("UTF-8"));

localGZIPOutputStream.close();

localByteArrayOutputStream.close();

for(int i = 0;i < localByteArrayOutputStream.toByteArray().length;i ++){

System.out.println(localByteArrayOutputStream.toByteArray()[i]);

}

输出为:

31 -117 8 0 0 0 0 0 0 -53 72 -51 -55 -55 47 -49 47 -54 73 1 0 -83 32 -21 -7 10

0 0 0

然后是Go版本:

var gzBf bytes.Buffer

gzSizeBf := bufio.NewWriterSize(&gzBf, len(str))

gz := gzip.NewWriter(gzSizeBf)

gz.Write([]byte(str))

gz.Flush()

gz.Close()

gzSizeBf.Flush()

GB := (&gzBf).Bytes()

for i := 0; i < len(GB); i++ {

fmt.Println(GB[i])

}

输出:

31 139 8 0 0 9 110 136 0 255 202 72 205 201 201 47 207 47 202 73 1 0 0 0 255

255 1 0 0 255 255 173 32 235 249 10 0 0 0

为什么?

我以为这可能是最初由这两种语言的不同字节读取方法引起的。但是我注意到0永远不能转换为9 []byte。

我写错了代码吗?有什么方法可以使我的Go程序获得与Java程序相同的输出?

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值