mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-29 04:57:02 +00:00
Merge pull request #260 from teo-tsirpanis/zip-file
Revert to using the BCL `ZipFile` class, without any NuGet package dependencies.
This commit is contained in:
commit
dcdf79dc3a
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Security.Principal;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
@ -11,14 +12,11 @@ namespace WelsonJS.Launcher
|
|||
{
|
||||
private string workingDirectory;
|
||||
private string instanceId;
|
||||
private readonly string entryFileName;
|
||||
private string entryFileName;
|
||||
private string scriptName;
|
||||
private readonly ZipExtractor zipExtractor;
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
zipExtractor = new ZipExtractor();
|
||||
|
||||
entryFileName = "bootstrap.bat";
|
||||
|
||||
InitializeComponent();
|
||||
|
@ -132,22 +130,17 @@ namespace WelsonJS.Launcher
|
|||
Directory.Delete(workingDirectory, true);
|
||||
}
|
||||
|
||||
// try to extact ZIP compressed file
|
||||
if (zipExtractor.Extract(filePath, workingDirectory))
|
||||
{
|
||||
// record the first deploy time
|
||||
RecordFirstDeployTime(workingDirectory);
|
||||
// try to extact ZIP file
|
||||
ZipFile.ExtractToDirectory(filePath, workingDirectory);
|
||||
|
||||
// follow the sub-directory
|
||||
workingDirectory = Program.GetWorkingDirectory(instanceId, true);
|
||||
// record the first deploy time
|
||||
RecordFirstDeployTime(workingDirectory);
|
||||
|
||||
// Run the application
|
||||
Program.RunCommandPrompt(workingDirectory, entryFileName, scriptName, cbUseSpecificScript.Checked, cbInteractiveServiceApp.Checked);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Failed to extract the ZIP file.");
|
||||
}
|
||||
// follow the sub-directory
|
||||
workingDirectory = Program.GetWorkingDirectory(instanceId, true);
|
||||
|
||||
// Run the appliction
|
||||
Program.RunCommandPrompt(workingDirectory, entryFileName, scriptName, cbUseSpecificScript.Checked, cbInteractiveServiceApp.Checked);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
|
@ -56,12 +56,12 @@
|
|||
<ApplicationIcon>favicon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
|
@ -103,7 +103,6 @@
|
|||
<DependentUpon>GlobalSettingsForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ResourceServer.cs" />
|
||||
<Compile Include="ZipExtractor.cs" />
|
||||
<EmbeddedResource Include="EnvForm.resx">
|
||||
<DependentUpon>EnvForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
|
|
@ -1,275 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WelsonJS.Launcher
|
||||
{
|
||||
public class ZipExtractor
|
||||
{
|
||||
class Extractor
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string FileName { get; set; }
|
||||
public string Path { get; set; }
|
||||
public Func<string, string, string> ExtractCommand { get; set; }
|
||||
public bool UseCmd { get; set; } = false;
|
||||
}
|
||||
|
||||
private readonly List<Extractor> AvailableExtractors;
|
||||
|
||||
public ZipExtractor()
|
||||
{
|
||||
// Searches the computer for any known third-party ZIP utilities.
|
||||
AvailableExtractors = new List<Extractor>{
|
||||
new Extractor
|
||||
{
|
||||
Name = "7z",
|
||||
FileName = "7z.exe",
|
||||
ExtractCommand = (src, dest) => $"x \"{src}\" -o\"{dest}\" -y"
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "WinRAR",
|
||||
FileName = "rar.exe",
|
||||
ExtractCommand = (src, dest) => $"x -o+ \"{src}\" \"{dest}\\\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "PeaZip",
|
||||
FileName = "peazip.exe",
|
||||
ExtractCommand = (src, dest) => $"-ext2simple \"{src}\" \"{dest}\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "WinZip",
|
||||
FileName = "wzunzip.exe",
|
||||
ExtractCommand = (src, dest) => $"-d \"{dest}\" \"{src}\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "ALZip",
|
||||
FileName = "ALZipcon.exe",
|
||||
ExtractCommand = (src, dest) => $"-x \"{src}\" \"{dest}\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "Bandizip",
|
||||
FileName = "Bandizip.exe",
|
||||
ExtractCommand = (src, dest) => $"x -o:\"{dest}\" \"{src}\" -y"
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "IZArc",
|
||||
FileName = "izarce.exe",
|
||||
ExtractCommand = (src, dest) => $"-d -p\"{dest}\" \"{src}\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "jar (Java SDK)",
|
||||
FileName = "jar.exe",
|
||||
ExtractCommand = (src, dest) => $"xf \"{src}\"",
|
||||
UseCmd = true
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "tar (Windows)", // Windows 10 build 17063 or later
|
||||
FileName = "tar.exe",
|
||||
ExtractCommand = (src, dest) => $"-xf \"{src}\" -C \"{dest}\""
|
||||
},
|
||||
new Extractor
|
||||
{
|
||||
Name = "unzip", // Info-ZIP, Cygwin
|
||||
FileName = "unzip.exe",
|
||||
ExtractCommand = (src, dest) => $"\"{src}\" -d \"{dest}\" -o"
|
||||
}
|
||||
};
|
||||
|
||||
Task.Run(() => CheckAvailableExtractors());
|
||||
}
|
||||
|
||||
public bool Extract(string filePath, string workingDirectory)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
throw new FileNotFoundException("The specified file does not exist.", filePath);
|
||||
|
||||
if (!filePath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
|
||||
throw new ArgumentException("The specified file is not a ZIP archive.");
|
||||
|
||||
if (!IsValidFile(filePath))
|
||||
throw new InvalidDataException("The specified file is not a valid ZIP archive.");
|
||||
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
|
||||
foreach (var extractor in AvailableExtractors.Where(e => e.Path != null))
|
||||
{
|
||||
if (RunProcess(extractor, filePath, workingDirectory, true))
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ExtractUsingPowerShell(filePath, workingDirectory, true))
|
||||
return true;
|
||||
|
||||
if (ExtractUsingShell(filePath, workingDirectory, true))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsValidFile(string filePath)
|
||||
{
|
||||
byte[] signature = new byte[4];
|
||||
|
||||
using (var fs = File.OpenRead(filePath))
|
||||
{
|
||||
if (fs.Length < 4)
|
||||
return false;
|
||||
|
||||
int bytesRead = fs.Read(signature, 0, 4);
|
||||
if (bytesRead < 4)
|
||||
return false;
|
||||
}
|
||||
|
||||
return signature.SequenceEqual(new byte[] { 0x50, 0x4B, 0x03, 0x04 });
|
||||
}
|
||||
|
||||
private void CheckAvailableExtractors()
|
||||
{
|
||||
var fileNames = AvailableExtractors.Select(e => e.FileName).ToList();
|
||||
|
||||
// Check PATH environment variable
|
||||
var paths = (Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator);
|
||||
foreach (var dir in paths)
|
||||
{
|
||||
foreach (var fileName in fileNames)
|
||||
{
|
||||
var path = Path.Combine(dir.Trim(), fileName);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var index = fileNames.IndexOf(fileName);
|
||||
var extractor = AvailableExtractors[index];
|
||||
extractor.Path = path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check common install locations
|
||||
var programDirs = new[]
|
||||
{
|
||||
Path.Combine(Program.GetAppDataPath(), "bin"), // find an extractor from APPDATA directory
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
|
||||
};
|
||||
|
||||
foreach (var rootDir in programDirs)
|
||||
{
|
||||
if (!Directory.Exists(rootDir))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(rootDir, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var fileName = Path.GetFileName(file);
|
||||
if (fileNames.Contains(fileName))
|
||||
{
|
||||
var index = fileNames.IndexOf(fileName);
|
||||
var extractor = AvailableExtractors[index];
|
||||
extractor.Path = file;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Trace.TraceInformation($"Ignored file or directory: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool RunProcess(string execFilePath, string arguments, string workingDirectory, bool useCmd = false, bool showConsole = false)
|
||||
{
|
||||
int exitCode = -1;
|
||||
|
||||
string fileName = useCmd ? "cmd.exe" : execFilePath;
|
||||
string adjustedArguments = useCmd ? $"/c \"{execFilePath}\" {arguments}" : arguments;
|
||||
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
Arguments = adjustedArguments,
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = !showConsole,
|
||||
RedirectStandardError = !showConsole,
|
||||
UseShellExecute = showConsole,
|
||||
CreateNoWindow = !showConsole
|
||||
};
|
||||
|
||||
using (var process = new Process { StartInfo = psi, EnableRaisingEvents = true })
|
||||
{
|
||||
process.Start();
|
||||
process.WaitForExit();
|
||||
exitCode = process.ExitCode;
|
||||
}
|
||||
|
||||
if (exitCode != 0)
|
||||
Trace.TraceWarning($"{fileName} exit with code {exitCode}");
|
||||
else
|
||||
Trace.TraceInformation($"{fileName} finished successfully");
|
||||
|
||||
return exitCode == 0;
|
||||
}
|
||||
|
||||
private bool RunProcess(Extractor extractor, string filePath, string workingDirectory, bool showConsole = false)
|
||||
{
|
||||
return RunProcess(extractor.Path,
|
||||
extractor.ExtractCommand(filePath, workingDirectory),
|
||||
workingDirectory,
|
||||
extractor.UseCmd,
|
||||
showConsole
|
||||
);
|
||||
}
|
||||
|
||||
private bool ExtractUsingPowerShell(string filePath, string workingDirectory, bool showConsole = false)
|
||||
{
|
||||
var escapedSrc = filePath.Replace("'", "''");
|
||||
var escapedDest = workingDirectory.Replace("'", "''");
|
||||
var script = $"Expand-Archive -LiteralPath '{escapedSrc}' -DestinationPath '{escapedDest}' -Force";
|
||||
|
||||
return RunProcess("powershell.exe", $"-NoProfile -Command \"{script}\"", workingDirectory);
|
||||
}
|
||||
|
||||
private bool ExtractUsingShell(string filePath, string workingDirectory, bool showConsole = false)
|
||||
{
|
||||
var shellAppType = Type.GetTypeFromProgID("Shell.Application");
|
||||
if (shellAppType == null)
|
||||
return false;
|
||||
|
||||
dynamic shell = Activator.CreateInstance(shellAppType);
|
||||
if (shell == null)
|
||||
return false;
|
||||
|
||||
dynamic zip = shell.NameSpace(filePath);
|
||||
dynamic dest = shell.NameSpace(workingDirectory);
|
||||
if (zip == null || dest == null)
|
||||
return false;
|
||||
|
||||
int expected = zip.Items().Count;
|
||||
dest.CopyHere(zip.Items(), 16);
|
||||
|
||||
// wait (max 30 s) until all files appear
|
||||
var sw = Stopwatch.StartNew();
|
||||
while (dest.Items().Count < expected && sw.Elapsed < TimeSpan.FromSeconds(30))
|
||||
System.Threading.Thread.Sleep(200);
|
||||
|
||||
Marshal.ReleaseComObject(zip);
|
||||
Marshal.ReleaseComObject(dest);
|
||||
Marshal.ReleaseComObject(shell);
|
||||
|
||||
return dest.Items().Count == expected;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user