C# 到 Java:Base64String、MemoryStream、GZipStream

发布于 2024-08-04 14:52:05 字数 820 浏览 9 评论 0原文

我有一个在 .NET 中压缩的 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);
     }
}

任何指针都值得赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-08-11 14:52:05

对于 Base64,您有 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");
}

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

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

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.

ヅ她的身影、若隐若现 2024-08-11 14:52:05

对于 Base64,我推荐 iHolder 的实现

GZipinputStream 是解压缩 GZip 字节数组需要什么。

ByteArrayOutputStream 用于将字节写入内存。然后,您获取字节并将它们传递给字符串对象的构造函数以对其进行转换,最好指定编码。

For Base64, I recommend iHolder's implementation.

GZipinputStream is what you need to decompress a GZip byte array.

ByteArrayOutputStream is what you use to write out bytes to memory. You then get the bytes and pass them to the constructor of a string object to convert them, preferably specifying the encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文