C# .Net Core Zip压缩包中文名乱码的解决方法

项目中使用ICSharpCode.SharpZipLib.Zip库进行解压,之前自动更新程序是.NET 4.5的,升级到.NET 8后,发现解压升级包里面的中文文件名是乱码了,经过一番摸索,增加一句代码可以解决乱码问题:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

cs 复制代码
public static bool DeCompressionZip(string _depositPath, string _floderPath)
{
  
	if (!Directory.Exists(_floderPath))
	{
		Directory.CreateDirectory(_floderPath);
	}

	Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);// 解决文件名中文乱码
	
	using (ZipInputStream s = new ZipInputStream(File.OpenRead(_depositPath)))
	{
		ZipEntry ze;
		while ((ze = s.GetNextEntry()) != null) //如果解压完ze则是null
		{
			if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名   
			{   
				string zeName = ze.Name;
				string directoryName = Path.GetDirectoryName(zeName);
				// create directory
				if (!string.IsNullOrEmpty(directoryName) && directoryName.Length > 0)
				{
					directoryName = Path.Combine(_floderPath, directoryName);
					if (!Directory.Exists(directoryName))
					{
						Directory.CreateDirectory(directoryName);
					}
				}
				string fileName = Path.GetFileName(zeName);
				if (!string.IsNullOrEmpty(fileName))
				{
					using (FileStream streamWriter = File.OpenWrite(Path.Combine(_floderPath, zeName)))
					{
						int size = 2048;
						byte[] data = new byte[2048];
						while (true)
						{
							size = s.Read(data, 0, data.Length);
							if (size <= 0)
							{
								break;
							}
							streamWriter.Write(data, 0, size);
						}
					}
				}
			}
		}
	}
	return true;
}
相关推荐
暖馒7 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
刘欣的博客10 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
Yorlen_Zhang11 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝19112 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话12 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc114007566313 小时前
通信 28
c#
永远是我的最爱15 小时前
基于.NET的小小便利店前台收银系统
前端·sqlserver·.net·visual studio
菜鸟特工00717 小时前
javax.net.ssl.SSLPeerUnverifiedException 异常如何处理
网络协议·.net·ssl
bugcome_com17 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中17 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#