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;
}
相关推荐
晨星shine7 小时前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户2986985301415 小时前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户36674625267416 小时前
接口文档汇总 - 2.设备状态管理
c#
用户36674625267416 小时前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
赵榕2 天前
ClaimsPrincipal序列化为Json的正确姿势
.net
追逐时光者2 天前
一款使用 C# 编写专为 Windows 11 打造的文件资源管理器增强工具!
后端·.net
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf