我有一个Base64string这是被压缩。净额与我想转换成一串。我在找一些Java等C#法,特别是:

  • 转换。FromBase64String
  • 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,你有 Base64 从Apache Commons, decodeBase64 方法需要一个 String 和返回 byte[].

然后,你可以看得到的 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");
}

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

其他提示

为Base64,我建议 iHolder的执行情况.

GZipinputStream 是你需要解GZip字阵列。

协议的版本为什么你用它来写字节的记忆。然后你的字节,并通过他们的构造的一串的对象,将它们转换,最好指定编码。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top