java memorystream 包_C#到Java:Base64String,MemoryStream,GZipStream

我有一个在.NET中被gzip压缩的Base64字符串,我想将它转换回Java中的字符串.我正在寻找C#语法的一些Java等价物,特别是:

> Convert.FromBase64String

> MemoryStream

> GZipStream

这是我要转换的方法:

public static string Decompress(string zipText) {

byte[] gzipBuff = Convert.FromBase64String(zipText);

using (MemoryStream memstream = new MemoryStream())

{

int msgLength = BitConverter.ToInt32(gzipBuff, 0);

memstream.Write(gzipBuff, 4, gzipBuff.Length - 4);

byte[] buffer = new byte[msgLength];

memstream.Position = 0;

using (GZipStream gzip = new GZipStream(memstream, CompressionMode.Decompress))

{

gzip.Read(buffer, 0, buffer.Length);

}

return Encoding.UTF8.GetString(buffer);

}

}

任何指针都表示赞赏.

解决方法:

对于Base64,您拥有Apache Commons的Base64 class和带有String并返回byte []的decodeBase64方法.

然后,您可以将生成的byte []读入ByteArrayInputStream.最后,将ByteArrayInputStream传递给GZipInputStream并读取未压缩的字节.

代码看起来像这样的东西:

public static String Decompress(String zipText) throws IOException {

byte[] gzipBuff = Base64.decodeBase64(zipText);

ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff);

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");

}

我没有测试代码,但我认为它应该可以工作,也许只需要一些修改.

标签:c,java,memorystream,gzipstream,gzipinputstream

来源: https://codeday.me/bug/20190622/1259920.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值