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

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

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

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;
}

相关推荐

  1. C# .Net Core Zip压缩中文解决方法

    2024-07-13 17:54:03       22 阅读
  2. Vim 编辑文件时中文解决方法

    2024-07-13 17:54:03       18 阅读
  3. pycharm控制台中文显示解决方案

    2024-07-13 17:54:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 17:54:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 17:54:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 17:54:03       58 阅读
  4. Python语言-面向对象

    2024-07-13 17:54:03       69 阅读

热门阅读

  1. live555关于RTSP协议交互流程

    2024-07-13 17:54:03       15 阅读
  2. EXPORT_SYMBOL

    2024-07-13 17:54:03       24 阅读
  3. 【车载开发系列】汽车开发常见概念理解

    2024-07-13 17:54:03       19 阅读
  4. 深入理解Spring Boot中的定时任务调度

    2024-07-13 17:54:03       17 阅读
  5. 大数据平台建设概要

    2024-07-13 17:54:03       21 阅读
  6. python文件

    2024-07-13 17:54:03       22 阅读
  7. python运行环境在新旧电脑间迁移

    2024-07-13 17:54:03       20 阅读
  8. LeetCode题练习与总结:最小栈--155

    2024-07-13 17:54:03       17 阅读
  9. C++catch (...)陈述

    2024-07-13 17:54:03       16 阅读