From 28c61ed7313d1162352a64864f326c5b4341b574 Mon Sep 17 00:00:00 2001
From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com>
Date: Wed, 20 Nov 2024 23:38:22 +0100
Subject: [PATCH] Move from DotNetZip to SharpCompress
---
src/KKManager.Core/Data/Zipmods/Manifest.cs | 8 +--
.../Data/Zipmods/SideloaderModLoader.cs | 70 ++++++-------------
src/KKManager.Core/KKManager.Core.csproj | 3 -
src/KKManager.Core/packages.config | 1 -
src/KKManager/KKManager.csproj | 3 -
.../ModpackTool/Data/ZipmodProcessor.cs | 47 +++++--------
src/KKManager/packages.config | 1 -
7 files changed, 44 insertions(+), 89 deletions(-)
diff --git a/src/KKManager.Core/Data/Zipmods/Manifest.cs b/src/KKManager.Core/Data/Zipmods/Manifest.cs
index b1759a65..68d1414a 100644
--- a/src/KKManager.Core/Data/Zipmods/Manifest.cs
+++ b/src/KKManager.Core/Data/Zipmods/Manifest.cs
@@ -5,10 +5,10 @@
using System.Linq;
using System.Xml;
using System.Xml.Linq;
-using Ionic.Zip;
using KKManager.Util;
using SharpCompress.Archives;
using System.Diagnostics;
+using SharpCompress.Archives.Zip;
#if AI || HS2
using AIChara;
#endif
@@ -239,11 +239,11 @@ internal static bool TryLoadFromZip(IArchive zip, out Manifest manifest)
}
}
- internal static Manifest LoadFromZip(ZipFile zip)
+ internal static Manifest LoadFromZip(ZipArchive zip)
{
- var entry = zip.Entries.FirstOrDefault(x => !x.IsDirectory && x.FileName == "manifest.xml");
+ var entry = zip.Entries.FirstOrDefault(x => !x.IsDirectory && string.Equals(x.Key, "manifest.xml", StringComparison.OrdinalIgnoreCase));
- return LoadFromZipInt(entry?.OpenReader());
+ return LoadFromZipInt(entry?.OpenEntryStream());
}
internal static Manifest LoadFromZip(IArchive zip)
diff --git a/src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs b/src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs
index a5653b47..092e33b0 100644
--- a/src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs
+++ b/src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs
@@ -5,16 +5,13 @@
using System.IO;
using System.Linq;
using System.Reactive.Subjects;
-using System.Reflection;
using System.Runtime;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
-using Ionic.Zip;
-using Ionic.Zlib;
using KKManager.Functions;
-using SharpCompress.Common;
+using SharpCompress.Archives.Zip;
using Sideloader;
namespace KKManager.Data.Zipmods
@@ -75,7 +72,7 @@ void ReadSideloaderModsAsync()
}
var files = Directory.EnumerateFiles(modDirectory, "*.*", searchOption);
- Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken}, file =>
+ Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken }, file =>
{
try
{
@@ -139,11 +136,10 @@ public static SideloaderModInfo LoadFromFile(string filename)
if (!IsValidZipmodExtension(location.Extension))
throw new ArgumentException($"The file {filename} has an invalid extension and can't be a zipmod", nameof(filename));
- using (var zf = new ZipFile())
+ using (var zf = ZipArchive.Open(location))
{
// Without this reading crashes if any entry name has invalid characters
- zf.IgnoreDuplicateFiles = true;
- zf.Initialize(location.FullName);
+ // TODO not available in sharplib - zf.IgnoreDuplicateFiles = true;
var manifest = Manifest.LoadFromZip(zf);
@@ -156,69 +152,45 @@ public static SideloaderModInfo LoadFromFile(string filename)
{
try
{
- return x.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
- x.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase);
+ return x.Key != null && (x.Key.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
+ x.Key.EndsWith(".png", StringComparison.OrdinalIgnoreCase));
}
catch (Exception e)
{
// Handle entries with invalid characters in filename
- Console.WriteLine($"WARN: Zipmod={location.Name} Entry={x.FileName} Error={e.Message}");
+ Console.WriteLine($"WARN: Zipmod={location.Name} Entry={x.Key} Error={e.Message}");
return false;
}
})
- .OrderBy(x => x.FileName).Take(5))
+ .OrderBy(x => x.Key).Take(5))
{
- var imgName = imageFile.FileName;
+ var imgName = imageFile.Key;
- if (imageFile.CompressionLevel == CompressionLevel.None)
+ images.Add(() =>
{
- var prop = typeof(ZipEntry).GetProperty("FileDataPosition", BindingFlags.Instance | BindingFlags.NonPublic);
- if (prop == null) throw new ArgumentNullException(nameof(prop));
- var pos = (long)prop.GetValue(imageFile, null);
- images.Add(() =>
+ try
{
- try
+ using (var zf2 = ZipArchive.Open(location))
{
- using (var archiveStream = new FileStream(location.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
+ var if2 = zf2.Entries.First(x => x.Key == imgName);
+ using (var archiveStream = if2.OpenEntryStream())
{
- archiveStream.Position = pos;
using (var img = Image.FromStream(archiveStream))
{
return img.GetThumbnailImage(200, 200, null, IntPtr.Zero);
}
}
}
- catch (SystemException ex)
- {
- Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
- return null;
- }
- });
- }
- else
- {
- images.Add(() =>
+ }
+ catch (SystemException ex)
{
- try
- {
- using (var archiveStream = new FileStream(location.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
- using (var archive = ZipFile.Read(archiveStream))
- using (var imgStream = archive.Entries.First(x => x.FileName == imgName).OpenReader())
- using (var img = Image.FromStream(imgStream))
- {
- return img.GetThumbnailImage(200, 200, null, IntPtr.Zero);
- }
- }
- catch (SystemException ex)
- {
- Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
- return null;
- }
- });
- }
+ Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
+ return null;
+ }
+ });
}
- var contents = zf.Entries.Where(x => !x.IsDirectory).Select(x => x.FileName.Replace('/', '\\')).ToList();
+ var contents = zf.Entries.Where(x => !x.IsDirectory).Select(x => x.Key?.Replace('/', '\\')).ToList();
return new SideloaderModInfo(location, manifest, images, contents);
}
diff --git a/src/KKManager.Core/KKManager.Core.csproj b/src/KKManager.Core/KKManager.Core.csproj
index 4f478e85..2c1eea94 100644
--- a/src/KKManager.Core/KKManager.Core.csproj
+++ b/src/KKManager.Core/KKManager.Core.csproj
@@ -55,9 +55,6 @@
..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll
-
- ..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll
-
..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll
diff --git a/src/KKManager.Core/packages.config b/src/KKManager.Core/packages.config
index ae6e862f..8c1d8424 100644
--- a/src/KKManager.Core/packages.config
+++ b/src/KKManager.Core/packages.config
@@ -1,7 +1,6 @@
-
diff --git a/src/KKManager/KKManager.csproj b/src/KKManager/KKManager.csproj
index a91da3ff..a4642bfb 100644
--- a/src/KKManager/KKManager.csproj
+++ b/src/KKManager/KKManager.csproj
@@ -66,9 +66,6 @@
..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll
-
- ..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll
-
..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll
diff --git a/src/KKManager/ModpackTool/Data/ZipmodProcessor.cs b/src/KKManager/ModpackTool/Data/ZipmodProcessor.cs
index 8259782a..62e72b8a 100644
--- a/src/KKManager/ModpackTool/Data/ZipmodProcessor.cs
+++ b/src/KKManager/ModpackTool/Data/ZipmodProcessor.cs
@@ -5,15 +5,18 @@
using System.IO;
using System.Linq;
using System.Reflection;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
-using Ionic.Zip;
using KKManager.Data.Cards;
using KKManager.SB3UGS;
using KKManager.Util;
using SharpCompress.Archives;
-using CompressionLevel = Ionic.Zlib.CompressionLevel;
+using SharpCompress.Archives.Zip;
+using SharpCompress.Common;
+using SharpCompress.Compressors.Deflate;
+using SharpCompress.Writers.Zip;
namespace KKManager.ModpackTool
{
@@ -131,35 +134,23 @@ private static async Task ProcessingThread()
manifestWorkCopy.Save(writer, SaveOptions.OmitDuplicateNamespaces);
// ------ Recompress the archive
- //using (var zf = ZipArchive.Create())
- //using (var writeStream = File.OpenWrite(outPath))
- //{
- // zf.DeflateCompressionLevel = CompressionLevel.None;
- // zf.AddAllFromDirectory(tempDir);
- // zf.SaveTo(writeStream, new ZipWriterOptions(CompressionType.None)
- // {
- // //ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8),
- // //ArchiveComment = archiveComment
- // });
- //}
-
- using (var writeStream = File.OpenWrite(outPath))
- using (var zf = new Ionic.Zip.ZipFile())
+ using (var zf = ZipArchive.Create())
+ using (var fs = File.Create(outPath))
{
- zf.CompressionMethod = CompressionMethod.None;
- zf.CompressionLevel = CompressionLevel.None;
- zf.Comment = archiveComment;
-
- zf.AddDirectory(tempDir, "");
- zf.Save(writeStream);
+ zf.DeflateCompressionLevel = CompressionLevel.None;
+ zf.AddAllFromDirectory(tempDir);
+ zf.SaveTo(fs, new ZipWriterOptions(CompressionType.Deflate)
+ {
+ ArchiveComment = archiveComment,
+ ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8),
+ // Seems like there's soem funny business going on (getting zip version 63 instead of 20) and setting both of these again seems to fix it???
+ CompressionType = CompressionType.Deflate,
+ DeflateCompressionLevel = CompressionLevel.None,
+ UseZip64 = false,
+ LeaveStreamOpen = false
+ });
}
-
-
-
-
-
-
// ------ Clean up
await new DirectoryInfo(tempDir).SafeDelete();
diff --git a/src/KKManager/packages.config b/src/KKManager/packages.config
index cbbcf54f..ed90801a 100644
--- a/src/KKManager/packages.config
+++ b/src/KKManager/packages.config
@@ -3,7 +3,6 @@
-