天天看點

【項目分析】利用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的方法實作為:

【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼
【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼

代碼

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

}

得出的結果為:

【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼

這個得到具有一定協定的資料格式,這是項目制定的,這裡不必多說。

現在我們來看下C#該如何實作它:

【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼
【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼

        [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中。

【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼

可以發現得到的結果是和JAVA版一樣的,程式得到實作。