C#与Java:Base64String,MemoryStream的,GZipStream

编程入门 行业动态 更新时间:2024-10-25 20:20:33
本文介绍了C#与Java:Base64String,MemoryStream的,GZipStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个在.NET中被gzip压缩的一个Base64字符串,我想将其转换回Java中的字符串。我在寻找一些Java等效于C#语法,尤其是:

I have a Base64 string that's been gzipped in .NET and I would like to convert it back into a string in Java. I'm looking for some Java equivalents to the C# syntax, particularly:

  • Convert.FromBase64String
  • 的MemoryStream
  • GZipStream

下面是我想转换的方法:

Here's the method I'd like to convert:

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

任何指针赞赏。

Any pointers are appreciated.

推荐答案

有关Base64的,你有的Base64 的来自Apache下议院类,而 decodeBase64 方法,它需要一个字符串并返回字节[] 。

For Base64, you have the Base64 class from Apache Commons, and the decodeBase64 method which takes a String and returns a byte[].

然后,您可以读取结果字节[] 成的 ByteArrayInputStream的 。最后,通过 ByteArrayInputStream的到 GZipInputStream 并读取压缩字节。

Then, you can read the resulting byte[] into a ByteArrayInputStream. At last, pass the ByteArrayInputStream to a GZipInputStream and read the uncompressed bytes.

代码看起来像这些方针的东西:

The code looks like something along these lines:

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

我没有测试代码,但我认为它应该工作,也许有一些修改。

I didn't test the code, but I think it should work, maybe with a few modifications.

更多推荐

C#与Java:Base64String,MemoryStream的,GZipStream

本文发布于:2023-11-26 19:32:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Base64String   Java   GZipStream   MemoryStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!