【修改】 解压文件中文名称乱码问题
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MySql.Data" Version="9.3.0" />
|
<PackageReference Include="MySql.Data" Version="9.3.0" />
|
||||||
|
<PackageReference Include="SharpZipLib" Version="1.4.2" />
|
||||||
<PackageReference Include="System.ServiceProcess.ServiceController" Version="9.0.6" />
|
<PackageReference Include="System.ServiceProcess.ServiceController" Version="9.0.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@@ -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.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -9,12 +13,9 @@ using System.Net.NetworkInformation;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Applications;
|
|
||||||
using ICSharpCode.SharpZipLib.Zip;
|
|
||||||
using Microsoft.Win32;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
@@ -37,7 +38,7 @@ class Program
|
|||||||
// 添加 CORS 响应头
|
// 添加 CORS 响应头
|
||||||
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
||||||
context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
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")
|
if (context.Request.HttpMethod == "OPTIONS")
|
||||||
@@ -136,7 +137,8 @@ class Program
|
|||||||
{
|
{
|
||||||
Console.WriteLine("检测到ZIP文件,开始解压...");
|
Console.WriteLine("检测到ZIP文件,开始解压...");
|
||||||
string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath));
|
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}");
|
Console.WriteLine($"解压完成,文件保存在: {extractPath}");
|
||||||
|
|
||||||
// 删除原ZIP文件
|
// 删除原ZIP文件
|
||||||
@@ -649,7 +651,68 @@ class Program
|
|||||||
}
|
}
|
||||||
return false; // 端口可用
|
return false; // 端口可用
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 解压ZIP文件并保留中文文件名
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="zipFilePath">ZIP文件路径</param>
|
||||||
|
/// <param name="outputFolder">解压目标文件夹</param>
|
||||||
|
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<string> DownloadFileAsync(string url, string saveDir)
|
static async Task<string> DownloadFileAsync(string url, string saveDir)
|
||||||
{
|
{
|
||||||
using (var httpClient = new HttpClient())
|
using (var httpClient = new HttpClient())
|
||||||
|
Reference in New Issue
Block a user