zoukankan      html  css  js  c++  java
  • 利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中。

    在JAVA程序中,有个HTTP请求数据头中,包含一个BASE64编码的字符串,例如:

    eJyVjMENgDAMA1fpBMjnIkp3ZzZEpAa1PLmXY10sDdqBqr54Ww5AthG7zxJYa0MYr9p7bPFnK/uqjCj06y7JfHwAX3AhhA==

    现在需要将这个字符串转化成原始字符串,原始字符串包含许多重要的信息。

    我们来看下JAVA是如何实现这个程序的:

    String str = "……";
    System.out.println(new String(ZipUtil.decompressByteArray(Base64.decodeBase64(str.getBytes()))));

    其中Base64为commons-codec-1.3.jar包中的一个类。这个包主要包括核心的算法,比如MD5,SHA1等等,还有一些常规加密解密的算法。

    而ZipUtil.decompressByteArray的方法实现为:

    复制代码
    代码
    public static byte[] decompressByteArray(byte abyte0[]) 

        Inflater inflater = new Inflater(); 
        inflater.setInput(abyte0); 
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(abyte0.length); 
        byte abyte1[] = new byte[1024]; 
        while (!inflater.finished()) 
            try 
            { 
                int i = inflater.inflate(abyte1); 
                bytearrayoutputstream.write(abyte1, 0, i); 
            } 
            catch (DataFormatException dataformatexception) { } 
        try 
        { 
            bytearrayoutputstream.close(); 
        } 
        catch (IOException ioexception) { } 
        return bytearrayoutputstream.toByteArray(); 
    }
    复制代码

    得出的结果为:

    image

    这个得到具有一定协议的数据格式,这是项目制定的,这里不必多说。

    现在我们来看下C#该如何实现它:

    复制代码
    代码
            [Test]
            public void Base64Test()
            {
                string baseStr = "eJyVjMENgDAMA1fpBMjnIkp3ZzZEpAa1PLmXY10sDdqBqr54Ww5AthG7zxJYa0MYr9p7bPFnK/uqjCj06y7JfHwAX3AhhA==";
                // Base64解码
                byte[] baseBytes = Convert.FromBase64String(baseStr);
                // Inflater解压
                string resultStr = Decompress(baseBytes);

                Console.WriteLine(resultStr);
            }

            /// <summary>
            /// Inflater解压
            /// </summary>
            /// <param name="baseBytes"></param>
            /// <returns></returns>
            public string Decompress(byte[] baseBytes)
            {
                string resultStr = string.Empty;
                using (MemoryStream memoryStream = new MemoryStream(baseBytes))
                {
                    using (InflaterInputStream inf = new InflaterInputStream(memoryStream))
                    {
                        using (MemoryStream buffer = new MemoryStream())
                        {
                            byte[] result = new byte[1024];

                            int resLen;
                            while ((resLen = inf.Read(result, 0, result.Length)) > 0)
                            {
                                buffer.Write(result, 0, resLen);
                            }
                            resultStr = Encoding.Default.GetString(result);
                        }
                    }
                }
                return resultStr;
            }
    复制代码

    其中InflaterInputStream的类来自于ICSharpCode.SharpZipLib.dll中。

    得出的结果为:

    image

    可以发现得到的结果是和JAVA版一样的,程序得到实现。

  • 相关阅读:
    48. Rotate Image
    83. Remove Duplicates from Sorted List
    46. Permutations
    HTML5笔记
    18. 4Sum
    24. Swap Nodes in Pairs
    42. Trapping Rain Water
    Python modf() 函数
    Python min() 函数
    Python max() 函数
  • 原文地址:https://www.cnblogs.com/Alex80/p/6641937.html
Copyright © 2011-2022 走看看