From 1dbc67b3c423cfb6ffa3a25a4279b90035744880 Mon Sep 17 00:00:00 2001 From: dlaren Date: Mon, 14 Jul 2025 17:21:24 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E6=96=87=E4=BB=B6-=E6=8F=90=E4=BE=9B=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=EF=BC=8C=E8=BF=9B=E8=A1=8C=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E6=96=87=E4=BB=B6=E6=88=90zip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Program.cs | 66 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/Applications/Program.cs b/Applications/Program.cs index aa02c36..03f3909 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -12,6 +12,7 @@ using System.ServiceProcess; using System.Text.Json; using System.Xml.Linq; using Applications; +using ICSharpCode.SharpZipLib.Zip; using Microsoft.Win32; using MySql.Data.MySqlClient; @@ -58,9 +59,72 @@ class Program string filesurl = context.Request.QueryString["filesurl"]; // 文件名称 string desiredFileName = context.Request.QueryString["filename"]; + // 需要被打包的 源目录路径 + string sourceDir = context.Request.QueryString["sourceDir"]; string ip = context.Request.QueryString["ip"]; string action = context.Request.Url.AbsolutePath.ToLower(); string responseMessage = ""; + // 压缩文件方法 + if (action == "/zipfiles") + { + try + { + if (!Directory.Exists(sourceDir)) + { + throw new DirectoryNotFoundException("源目录不存在!"); + } + + // 构造ZIP文件路径(同级目录,同名.zip) + string parentDir = Directory.GetParent(sourceDir).FullName; + string folderName = new DirectoryInfo(sourceDir).Name; + string outputZipPath = Path.Combine(parentDir, $"{folderName}.zip"); + + // 如果ZIP已存在,先删除(可选) + if (File.Exists(outputZipPath)) + { + File.Delete(outputZipPath); + } + + // 使用SharpZipLib打包 + using (var zipStream = new ZipOutputStream(File.Create(outputZipPath))) + { + zipStream.SetLevel(5); // 压缩级别 (0-9) + + foreach (var file in Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories)) + { + string relativePath = file.Substring(sourceDir.Length + 1); + var entry = new ZipEntry(relativePath) + { + DateTime = File.GetLastWriteTime(file), + IsUnicodeText = true + }; + + // 保留文件属性(隐藏、只读等) + var fileAttributes = File.GetAttributes(file); + entry.ExternalFileAttributes = (int)fileAttributes << 16; + + zipStream.PutNextEntry(entry); + + using (var fileStream = File.OpenRead(file)) + { + fileStream.CopyTo(zipStream); + } + + zipStream.CloseEntry(); + } + } + + // 返回ZIP文件的绝对路径 + responseMessage = Path.GetFullPath(outputZipPath); + } + catch (Exception ex) + { + throw new Exception($"压缩失败: {ex.Message}"); + } + } + + + // 下载文件方法 if (action == "/downloadfiles") { try @@ -72,7 +136,7 @@ class Program { Console.WriteLine("检测到ZIP文件,开始解压..."); string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath)); - ZipFile.ExtractToDirectory(downloadedFilePath, folderPath); + System.IO.Compression.ZipFile.ExtractToDirectory(downloadedFilePath, folderPath); Console.WriteLine($"解压完成,文件保存在: {extractPath}"); // 删除原ZIP文件