【新增】压缩文件-提供文件路径,进行压缩文件成zip
This commit is contained in:
@@ -12,6 +12,7 @@ using System.ServiceProcess;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Applications;
|
using Applications;
|
||||||
|
using ICSharpCode.SharpZipLib.Zip;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
@@ -58,9 +59,72 @@ class Program
|
|||||||
string filesurl = context.Request.QueryString["filesurl"];
|
string filesurl = context.Request.QueryString["filesurl"];
|
||||||
// 文件名称
|
// 文件名称
|
||||||
string desiredFileName = context.Request.QueryString["filename"];
|
string desiredFileName = context.Request.QueryString["filename"];
|
||||||
|
// 需要被打包的 源目录路径
|
||||||
|
string sourceDir = context.Request.QueryString["sourceDir"];
|
||||||
string ip = context.Request.QueryString["ip"];
|
string ip = context.Request.QueryString["ip"];
|
||||||
string action = context.Request.Url.AbsolutePath.ToLower();
|
string action = context.Request.Url.AbsolutePath.ToLower();
|
||||||
string responseMessage = "";
|
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")
|
if (action == "/downloadfiles")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -72,7 +136,7 @@ class Program
|
|||||||
{
|
{
|
||||||
Console.WriteLine("检测到ZIP文件,开始解压...");
|
Console.WriteLine("检测到ZIP文件,开始解压...");
|
||||||
string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath));
|
string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath));
|
||||||
ZipFile.ExtractToDirectory(downloadedFilePath, folderPath);
|
System.IO.Compression.ZipFile.ExtractToDirectory(downloadedFilePath, folderPath);
|
||||||
Console.WriteLine($"解压完成,文件保存在: {extractPath}");
|
Console.WriteLine($"解压完成,文件保存在: {extractPath}");
|
||||||
|
|
||||||
// 删除原ZIP文件
|
// 删除原ZIP文件
|
||||||
|
Reference in New Issue
Block a user