From 92dc65fa4fafc878269d5e36fd885eaba396dc01 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 2 Jun 2023 10:08:07 +0800 Subject: [PATCH] x --- PublishSite/PublishForm.cs | 76 ++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/PublishSite/PublishForm.cs b/PublishSite/PublishForm.cs index 4193c831..25a7bc41 100644 --- a/PublishSite/PublishForm.cs +++ b/PublishSite/PublishForm.cs @@ -22,11 +22,14 @@ using System.IO; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; using System.Management.Automation.Host; using SharpCompress.Common; -using System.IO.Compression; using MaterialSkin2DotNet.Controls; using MaterialSkin2DotNet; using Renci.SshNet.Compression; using SharpCompress.Archives; +using System.Collections.ObjectModel; +using SharpCompress.Writers; +using SharpCompress.Writers.Zip; +using SharpCompress.Archives.Zip; namespace PublishSite { @@ -330,7 +333,7 @@ namespace PublishSite if (stopServiceCommand.ExitStatus != 0) { - WinformLog($"Failed to stop {serviceName}.",Color.Red); + WinformLog($"Failed to stop {serviceName}.", Color.Red); } Task.Delay(1000); @@ -643,7 +646,7 @@ namespace PublishSite { //startServiceBtn.Enabled = false; - + uiContext.Post((state) => { // 访问控件并更新它们 @@ -1027,49 +1030,58 @@ namespace PublishSite try { - //await Task.Run( () => - //{ - //将文件夹里的内容打包成压缩包 - //ZipFile.CreateFromDirectory(localSourceFolder, filePath,CompressionLevel.Fastest,true); + await Task.Run(() => + { + //ZipFile.CreateFromDirectory(localSourceFolder, filePath, CompressionLevel.Fastest, false); - //Compress - Archive - Path "C:\Users\username\Documents\myfolder" - DestinationPath "C:\Users\username\Documents\myfolder.zip" - Force - //var processStartInfo = new ProcessStartInfo + var files = Directory.GetFiles(localSourceFolder, "*", SearchOption.AllDirectories); + + using (var fs = new FileStream(outputPath, FileMode.Create)) + { + using (var archive = new ZipArchive()) + { + foreach (var file in files) + { + var entry = archive.AddEntry(Path.GetRelativePath(dirPath, file), file); + entry.CompressionType = CompressionType.Deflate; + } + + archive.SaveTo(fs, new WriterOptions(CompressionType.Deflate)); + } + } + //using (PowerShell powerShell = PowerShell.Create()) //{ - // FileName = "cmd.exe", - // RedirectStandardInput = true, - // UseShellExecute = false, - // CreateNoWindow = true - //}; + // // 设置PowerShell执行策略为Bypass以允许运行脚本 + // powerShell.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass"); - //using (var process = new Process { StartInfo = processStartInfo }) - //{ - // process.Start(); + // // 构造压缩命令 + // string compressCommand = String.Format("Compress-Archive -Path \"{0}\" -DestinationPath \"{1}\"", localSourceFolder, filePath); - // using (var writer = process.StandardInput) + // // 添加压缩命令到PowerShell对象中 + // powerShell.AddScript(compressCommand); + + // WinformLog(compressCommand); + + // try // { - // if (writer.BaseStream.CanWrite) + // // 执行PowerShell脚本 + // Collection results = powerShell.Invoke(); + + // foreach (var result in results) // { - // // 执行命令:Compress-Archive -Path -DestinationPath -Force - - // var cmdCompress = $"Compress-Archive -Path \"{localSourceFolder}\" -DestinationPath \"{filePath}\" -Force"; - // writer.WriteLine(cmdCompress); - - // WinformLog(cmdCompress); - // writer.Close(); + // WinformLog(result.ToString()); // } // } - - // process.WaitForExit(); - - // if (process.ExitCode != 0) + // catch (Exception ex) // { - // // 处理错误 + // WinformLog(ex.Message); // } //} + WinformLog("文件已成功压缩为 " + filePath); - //}); + }); WinformLog($"打包结束"); }