Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Cross-platform zip implementation (still uses 7z on windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Jan 12, 2022
1 parent 0f7c155 commit 1c096bc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/Squirrel/Internal/DeltaPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public ReleasePackage CreateDeltaPackage(ReleasePackage basePackage, ReleasePack
this.Log().Info("Extracting {0} and {1} into {2}",
basePackage.ReleasePackageFile, newPackage.ReleasePackageFile, tempPath);

HelperExe.ExtractZipToDirectory(basePackage.ReleasePackageFile, baseTempInfo.FullName).Wait();
HelperExe.ExtractZipToDirectory(newPackage.ReleasePackageFile, tempInfo.FullName).Wait();
EasyZip.ExtractZipToDirectory(basePackage.ReleasePackageFile, baseTempInfo.FullName);
EasyZip.ExtractZipToDirectory(newPackage.ReleasePackageFile, tempInfo.FullName);

// Collect a list of relative paths under 'lib' and map them
// to their full name. We'll use this later to determine in
Expand All @@ -86,7 +86,7 @@ public ReleasePackage CreateDeltaPackage(ReleasePackage basePackage, ReleasePack
}

ReleasePackage.addDeltaFilesToContentTypes(tempInfo.FullName);
HelperExe.CreateZipFromDirectory(outputFile, tempInfo.FullName).Wait();
EasyZip.CreateZipFromDirectory(outputFile, tempInfo.FullName);
}

return new ReleasePackage(outputFile);
Expand Down
84 changes: 84 additions & 0 deletions src/Squirrel/Internal/EasyZip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using Squirrel.SimpleSplat;

namespace Squirrel
{
internal class EasyZip
{
private static IFullLogger Log = SquirrelLocator.CurrentMutable.GetService<ILogManager>().GetLogger(typeof(EasyZip));

public static void ExtractZipToDirectory(string inputFile, string outputDirectory)
{
Log.Info($"Extracting '{inputFile}' to '{outputDirectory}'...");
if (Extract7z(inputFile, outputDirectory))
return;

using var archive = ZipArchive.Open(inputFile);
archive.WriteToDirectory(outputDirectory, new() {
PreserveFileTime = false,
Overwrite = true,
ExtractFullPath = true
});
}

public static void CreateZipFromDirectory(string outputFile, string directoryToCompress)
{
Log.Info($"Compressing '{directoryToCompress}' to '{outputFile}'...");
if (Compress7z(outputFile, directoryToCompress))
return;

using var archive = ZipArchive.Create();
archive.AddAllFromDirectory(directoryToCompress);
archive.SaveTo(outputFile, CompressionType.Deflate);
}

private static bool Extract7z(string zipFilePath, string outFolder)
{
#if !NETFRAMEWORK
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return false;
#endif

try {
var args = String.Format("x \"{0}\" -tzip -mmt on -aoa -y -o\"{1}\" *", zipFilePath, outFolder);
var psi = Utility.CreateProcessStartInfo(HelperExe.SevenZipPath, args);
var result = Utility.InvokeProcessUnsafeAsync(psi, CancellationToken.None)
.ConfigureAwait(false).GetAwaiter().GetResult();
if (result.ExitCode != 0) throw new Exception(result.StdOutput);
return true;
} catch (Exception ex) {
Log.Warn("Unable to extract archive with 7z.exe\n" + ex.Message);
return false;
}
}

private static bool Compress7z(string zipFilePath, string inFolder)
{
#if !NETFRAMEWORK
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return false;
#endif

try {
var args = String.Format("a \"{0}\" -tzip -aoa -y -mmt on *", zipFilePath);
var psi = Utility.CreateProcessStartInfo(HelperExe.SevenZipPath, args, inFolder);
var result = Utility.InvokeProcessUnsafeAsync(psi, CancellationToken.None)
.ConfigureAwait(false).GetAwaiter().GetResult();
if (result.ExitCode != 0) throw new Exception(result.StdOutput);
return true;
} catch (Exception ex) {
Log.Warn("Unable to create archive with 7z.exe\n" + ex.Message);
return false;
}
}
}
}
50 changes: 3 additions & 47 deletions src/Squirrel/Internal/HelperExe.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -21,10 +21,10 @@ internal static class HelperExe
public static string StubExecutablePath => FindHelperFile("StubExecutable.exe");
public static string SingleFileHostPath => FindHelperFile("singlefilehost.exe");
public static string WixTemplatePath => FindHelperFile("template.wxs");
public static string SevenZipPath => FindHelperFile("7z.exe");

// private so we don't expose paths to internal tools. these should be exposed as a helper function
private static string RceditPath => FindHelperFile("rcedit.exe");
private static string SevenZipPath => FindHelperFile("7z.exe");
private static string SignToolPath => FindHelperFile("signtool.exe");
private static string WixCandlePath => FindHelperFile("candle.exe");
private static string WixLightPath => FindHelperFile("light.exe");
Expand Down Expand Up @@ -73,7 +73,7 @@ private static string FindHelperFile(string toFind)
.FirstOrDefault(x => File.Exists(x));

if (result == null && throwWhenNotFound)
throw new Exception($"Could not find helper '{exe}'. If not in the default location, add additional search paths using command arguments.");
throw new Exception($"Could not find '{exe}'.");

return result ?? exe;
}
Expand Down Expand Up @@ -191,49 +191,5 @@ public static async Task SignPEFile(string exePath, string signingOpts)
Log.Info("Sign successful: " + processResult.StdOutput);
}
}

public static async Task ExtractZipToDirectory(string zipFilePath, string outFolder)
{
try {
var cmd = SevenZipPath;
var args = String.Format("x \"{0}\" -tzip -mmt on -aoa -y -o\"{1}\" *", zipFilePath, outFolder);

// TODO this should probably fall back to SharpCompress if not on windows
if (Environment.OSVersion.Platform != PlatformID.Win32NT) {
cmd = "wine";
args = SevenZipPath + " " + args;
}

var psi = Utility.CreateProcessStartInfo(cmd, args);

var result = await Utility.InvokeProcessUnsafeAsync(psi, CancellationToken.None).ConfigureAwait(false);
if (result.ExitCode != 0) throw new Exception(result.StdOutput);
} catch (Exception ex) {
Log.Error($"Failed to extract file {zipFilePath} to {outFolder}\n{ex.Message}");
throw;
}
}

public static async Task CreateZipFromDirectory(string zipFilePath, string inFolder)
{
try {
var cmd = SevenZipPath;
var args = String.Format("a \"{0}\" -tzip -aoa -y -mmt on *", zipFilePath);

// TODO this should probably fall back to SharpCompress if not on windows
if (Environment.OSVersion.Platform != PlatformID.Win32NT) {
cmd = "wine";
args = SevenZipPath + " " + args;
}

var psi = Utility.CreateProcessStartInfo(cmd, args, inFolder);

var result = await Utility.InvokeProcessUnsafeAsync(psi, CancellationToken.None).ConfigureAwait(false);
if (result.ExitCode != 0) throw new Exception(result.StdOutput);
} catch (Exception ex) {
Log.Error($"Failed to extract file {zipFilePath} to {inFolder}\n{ex.Message}");
throw;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Squirrel/Internal/ReleasePackage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -123,7 +123,7 @@ internal string CreateReleasePackage(string outputFile, Func<string, string> rel

contentsPostProcessHook?.Invoke(tempPath, package);

HelperExe.CreateZipFromDirectory(outputFile, tempPath).Wait();
EasyZip.CreateZipFromDirectory(outputFile, tempPath);

ReleasePackageFile = outputFile;
return ReleasePackageFile;
Expand Down

0 comments on commit 1c096bc

Please sign in to comment.