From 12e53ba75868893f3b6b969585803ecf3bec71ed Mon Sep 17 00:00:00 2001 From: dlaren Date: Tue, 29 Jul 2025 19:59:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=20?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E6=96=87=E4=BB=B6=E4=B8=AD=E6=96=87=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E4=B9=B1=E7=A0=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Applications.csproj | 1 + Applications/Program.cs | 77 +++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/Applications/Applications.csproj b/Applications/Applications.csproj index 9b77a94..fb70c6a 100644 --- a/Applications/Applications.csproj +++ b/Applications/Applications.csproj @@ -9,6 +9,7 @@ + diff --git a/Applications/Program.cs b/Applications/Program.cs index 03f3909..3f16dac 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -1,4 +1,8 @@ -using System; +using Applications; +using ICSharpCode.SharpZipLib.Zip; +using Microsoft.Win32; +using MySql.Data.MySqlClient; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; @@ -9,12 +13,9 @@ using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Security.Principal; using System.ServiceProcess; +using System.Text; using System.Text.Json; using System.Xml.Linq; -using Applications; -using ICSharpCode.SharpZipLib.Zip; -using Microsoft.Win32; -using MySql.Data.MySqlClient; class Program { @@ -37,7 +38,7 @@ class Program // 添加 CORS 响应头 context.Response.AddHeader("Access-Control-Allow-Origin", "*"); context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); - context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type"); + context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, tenant-id"); // 处理预检请求 if (context.Request.HttpMethod == "OPTIONS") @@ -136,7 +137,8 @@ class Program { Console.WriteLine("检测到ZIP文件,开始解压..."); string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath)); - System.IO.Compression.ZipFile.ExtractToDirectory(downloadedFilePath, folderPath); + //System.IO.Compression.ZipFile.ExtractToDirectory(downloadedFilePath, folderPath); + UnzipWithChineseNames(downloadedFilePath, folderPath); Console.WriteLine($"解压完成,文件保存在: {extractPath}"); // 删除原ZIP文件 @@ -649,7 +651,68 @@ class Program } return false; // 端口可用 } + /// + /// 解压ZIP文件并保留中文文件名 + /// + /// ZIP文件路径 + /// 解压目标文件夹 + public static void UnzipWithChineseNames(string zipFilePath, string outputFolder) + { + // ✅ 注册编码提供程序(仅.NET Core / .NET 5 + 需要) + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + // ✅ 设置 GBK 编码(或 UTF-8) + ZipStrings.CodePage = Encoding.GetEncoding("GBK").CodePage; // 或 936 / UTF8 + + if (!Directory.Exists(outputFolder)) + Directory.CreateDirectory(outputFolder); + + using (var fs = File.OpenRead(zipFilePath)) + using (var zipInputStream = new ZipInputStream(fs)) + { + ZipEntry entry; + while ((entry = zipInputStream.GetNextEntry()) != null) + { + // 处理路径(统一替换路径分隔符为当前系统的分隔符) + string entryName = entry.Name.Replace('/', Path.DirectorySeparatorChar); + string fullPath = Path.Combine(outputFolder, entryName); + + if (entry.IsDirectory) + { + // 如果是目录,确保创建该目录 + if (!Directory.Exists(fullPath)) + { + Directory.CreateDirectory(fullPath); + // 设置目录的修改时间(可选) + Directory.SetLastWriteTime(fullPath, entry.DateTime); + } + } + else + { + // 如果是文件,确保父目录存在 + string directoryName = Path.GetDirectoryName(fullPath); + if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName)) + { + Directory.CreateDirectory(directoryName); + } + + // 写入文件内容 + using (var streamWriter = File.Create(fullPath)) + { + byte[] buffer = new byte[4096]; + int size; + while ((size = zipInputStream.Read(buffer, 0, buffer.Length)) > 0) + { + streamWriter.Write(buffer, 0, size); + } + } + + // 设置文件的修改时间 + File.SetLastWriteTime(fullPath, entry.DateTime); + } + } + } + } static async Task DownloadFileAsync(string url, string saveDir) { using (var httpClient = new HttpClient())