Skip to content

Commit

Permalink
Move from DotNetZip to SharpCompress
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Nov 20, 2024
1 parent e915129 commit 28c61ed
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/KKManager.Core/Data/Zipmods/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
70 changes: 21 additions & 49 deletions src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
3 changes: 0 additions & 3 deletions src/KKManager.Core/KKManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
<Reference Include="Ben.Demystifier, Version=0.4.0.0, Culture=neutral, PublicKeyToken=a6d206e05440431a, processorArchitecture=MSIL">
<HintPath>..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll</HintPath>
</Reference>
<Reference Include="DotNetZip, Version=1.16.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="MessagePack, Version=2.5.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be, processorArchitecture=MSIL">
<HintPath>..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll</HintPath>
</Reference>
Expand Down
1 change: 0 additions & 1 deletion src/KKManager.Core/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Ben.Demystifier" version="0.4.1" targetFramework="net472" />
<package id="DotNetZip" version="1.16.0" targetFramework="net472" />
<package id="MessagePack" version="2.5.192" targetFramework="net472" />
<package id="MessagePack.Annotations" version="2.5.192" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.0" targetFramework="net472" />
Expand Down
3 changes: 0 additions & 3 deletions src/KKManager/KKManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@
<Reference Include="Ben.Demystifier, Version=0.4.0.0, Culture=neutral, PublicKeyToken=a6d206e05440431a, processorArchitecture=MSIL">
<HintPath>..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll</HintPath>
</Reference>
<Reference Include="DotNetZip, Version=1.16.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="MessagePack, Version=2.5.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be, processorArchitecture=MSIL">
<HintPath>..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll</HintPath>
</Reference>
Expand Down
47 changes: 19 additions & 28 deletions src/KKManager/ModpackTool/Data/ZipmodProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();

Expand Down
1 change: 0 additions & 1 deletion src/KKManager/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<package id="Ben.Demystifier" version="0.4.1" targetFramework="net472" />
<package id="DockPanelSuite" version="3.1.1" targetFramework="net472" />
<package id="DockPanelSuite.ThemeVS2015" version="3.1.1" targetFramework="net472" />
<package id="DotNetZip" version="1.16.0" targetFramework="net472" />
<package id="MessagePack" version="2.5.192" targetFramework="net472" />
<package id="MessagePack.Annotations" version="2.5.192" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.0" targetFramework="net472" />
Expand Down

0 comments on commit 28c61ed

Please sign in to comment.