【项目分析】利用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()))));
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();
}
{
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#该如何实现它:

[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;
}
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中。
得出的结果为:
可以发现得到的结果是和JAVA版一样的,程序得到实现。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
2010-02-23 【学习笔记】——网络营销5(能卖东西的网站)
2009-02-23 6分钟内听遍世界名曲。附带出处[英文版]
2007-02-23 IT从业人员必看的10个论坛(转)
2007-02-23 google map是怎样工作的