This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
forked from Squirrel/Squirrel.Windows
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cross-platform zip implementation (still uses 7z on windows)
- Loading branch information
Showing
4 changed files
with
92 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters