Skip to content

Commit 28c61ed

Browse files
committed
Move from DotNetZip to SharpCompress
1 parent e915129 commit 28c61ed

7 files changed

Lines changed: 44 additions & 89 deletions

File tree

src/KKManager.Core/Data/Zipmods/Manifest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System.Linq;
66
using System.Xml;
77
using System.Xml.Linq;
8-
using Ionic.Zip;
98
using KKManager.Util;
109
using SharpCompress.Archives;
1110
using System.Diagnostics;
11+
using SharpCompress.Archives.Zip;
1212
#if AI || HS2
1313
using AIChara;
1414
#endif
@@ -239,11 +239,11 @@ internal static bool TryLoadFromZip(IArchive zip, out Manifest manifest)
239239
}
240240
}
241241

242-
internal static Manifest LoadFromZip(ZipFile zip)
242+
internal static Manifest LoadFromZip(ZipArchive zip)
243243
{
244-
var entry = zip.Entries.FirstOrDefault(x => !x.IsDirectory && x.FileName == "manifest.xml");
244+
var entry = zip.Entries.FirstOrDefault(x => !x.IsDirectory && string.Equals(x.Key, "manifest.xml", StringComparison.OrdinalIgnoreCase));
245245

246-
return LoadFromZipInt(entry?.OpenReader());
246+
return LoadFromZipInt(entry?.OpenEntryStream());
247247
}
248248

249249
internal static Manifest LoadFromZip(IArchive zip)

src/KKManager.Core/Data/Zipmods/SideloaderModLoader.cs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
using System.IO;
66
using System.Linq;
77
using System.Reactive.Subjects;
8-
using System.Reflection;
98
using System.Runtime;
109
using System.Security;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using System.Windows.Forms;
14-
using Ionic.Zip;
15-
using Ionic.Zlib;
1613
using KKManager.Functions;
17-
using SharpCompress.Common;
14+
using SharpCompress.Archives.Zip;
1815
using Sideloader;
1916

2017
namespace KKManager.Data.Zipmods
@@ -75,7 +72,7 @@ void ReadSideloaderModsAsync()
7572
}
7673

7774
var files = Directory.EnumerateFiles(modDirectory, "*.*", searchOption);
78-
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken}, file =>
75+
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken }, file =>
7976
{
8077
try
8178
{
@@ -139,11 +136,10 @@ public static SideloaderModInfo LoadFromFile(string filename)
139136
if (!IsValidZipmodExtension(location.Extension))
140137
throw new ArgumentException($"The file {filename} has an invalid extension and can't be a zipmod", nameof(filename));
141138

142-
using (var zf = new ZipFile())
139+
using (var zf = ZipArchive.Open(location))
143140
{
144141
// Without this reading crashes if any entry name has invalid characters
145-
zf.IgnoreDuplicateFiles = true;
146-
zf.Initialize(location.FullName);
142+
// TODO not available in sharplib - zf.IgnoreDuplicateFiles = true;
147143

148144
var manifest = Manifest.LoadFromZip(zf);
149145

@@ -156,69 +152,45 @@ public static SideloaderModInfo LoadFromFile(string filename)
156152
{
157153
try
158154
{
159-
return x.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
160-
x.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase);
155+
return x.Key != null && (x.Key.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
156+
x.Key.EndsWith(".png", StringComparison.OrdinalIgnoreCase));
161157
}
162158
catch (Exception e)
163159
{
164160
// Handle entries with invalid characters in filename
165-
Console.WriteLine($"WARN: Zipmod={location.Name} Entry={x.FileName} Error={e.Message}");
161+
Console.WriteLine($"WARN: Zipmod={location.Name} Entry={x.Key} Error={e.Message}");
166162
return false;
167163
}
168164
})
169-
.OrderBy(x => x.FileName).Take(5))
165+
.OrderBy(x => x.Key).Take(5))
170166
{
171-
var imgName = imageFile.FileName;
167+
var imgName = imageFile.Key;
172168

173-
if (imageFile.CompressionLevel == CompressionLevel.None)
169+
images.Add(() =>
174170
{
175-
var prop = typeof(ZipEntry).GetProperty("FileDataPosition", BindingFlags.Instance | BindingFlags.NonPublic);
176-
if (prop == null) throw new ArgumentNullException(nameof(prop));
177-
var pos = (long)prop.GetValue(imageFile, null);
178-
images.Add(() =>
171+
try
179172
{
180-
try
173+
using (var zf2 = ZipArchive.Open(location))
181174
{
182-
using (var archiveStream = new FileStream(location.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
175+
var if2 = zf2.Entries.First(x => x.Key == imgName);
176+
using (var archiveStream = if2.OpenEntryStream())
183177
{
184-
archiveStream.Position = pos;
185178
using (var img = Image.FromStream(archiveStream))
186179
{
187180
return img.GetThumbnailImage(200, 200, null, IntPtr.Zero);
188181
}
189182
}
190183
}
191-
catch (SystemException ex)
192-
{
193-
Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
194-
return null;
195-
}
196-
});
197-
}
198-
else
199-
{
200-
images.Add(() =>
184+
}
185+
catch (SystemException ex)
201186
{
202-
try
203-
{
204-
using (var archiveStream = new FileStream(location.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
205-
using (var archive = ZipFile.Read(archiveStream))
206-
using (var imgStream = archive.Entries.First(x => x.FileName == imgName).OpenReader())
207-
using (var img = Image.FromStream(imgStream))
208-
{
209-
return img.GetThumbnailImage(200, 200, null, IntPtr.Zero);
210-
}
211-
}
212-
catch (SystemException ex)
213-
{
214-
Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
215-
return null;
216-
}
217-
});
218-
}
187+
Console.WriteLine($"Failed to load image \"{imgName}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
188+
return null;
189+
}
190+
});
219191
}
220192

221-
var contents = zf.Entries.Where(x => !x.IsDirectory).Select(x => x.FileName.Replace('/', '\\')).ToList();
193+
var contents = zf.Entries.Where(x => !x.IsDirectory).Select(x => x.Key?.Replace('/', '\\')).ToList();
222194

223195
return new SideloaderModInfo(location, manifest, images, contents);
224196
}

src/KKManager.Core/KKManager.Core.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
<Reference Include="Ben.Demystifier, Version=0.4.0.0, Culture=neutral, PublicKeyToken=a6d206e05440431a, processorArchitecture=MSIL">
5656
<HintPath>..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll</HintPath>
5757
</Reference>
58-
<Reference Include="DotNetZip, Version=1.16.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
59-
<HintPath>..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll</HintPath>
60-
</Reference>
6158
<Reference Include="MessagePack, Version=2.5.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be, processorArchitecture=MSIL">
6259
<HintPath>..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll</HintPath>
6360
</Reference>

src/KKManager.Core/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Ben.Demystifier" version="0.4.1" targetFramework="net472" />
4-
<package id="DotNetZip" version="1.16.0" targetFramework="net472" />
54
<package id="MessagePack" version="2.5.192" targetFramework="net472" />
65
<package id="MessagePack.Annotations" version="2.5.192" targetFramework="net472" />
76
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.0" targetFramework="net472" />

src/KKManager/KKManager.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@
6666
<Reference Include="Ben.Demystifier, Version=0.4.0.0, Culture=neutral, PublicKeyToken=a6d206e05440431a, processorArchitecture=MSIL">
6767
<HintPath>..\packages\Ben.Demystifier.0.4.1\lib\net45\Ben.Demystifier.dll</HintPath>
6868
</Reference>
69-
<Reference Include="DotNetZip, Version=1.16.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
70-
<HintPath>..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll</HintPath>
71-
</Reference>
7269
<Reference Include="MessagePack, Version=2.5.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be, processorArchitecture=MSIL">
7370
<HintPath>..\packages\MessagePack.2.5.192\lib\net472\MessagePack.dll</HintPath>
7471
</Reference>

src/KKManager/ModpackTool/Data/ZipmodProcessor.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
using System.IO;
66
using System.Linq;
77
using System.Reflection;
8+
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using System.Xml.Linq;
11-
using Ionic.Zip;
1212
using KKManager.Data.Cards;
1313
using KKManager.SB3UGS;
1414
using KKManager.Util;
1515
using SharpCompress.Archives;
16-
using CompressionLevel = Ionic.Zlib.CompressionLevel;
16+
using SharpCompress.Archives.Zip;
17+
using SharpCompress.Common;
18+
using SharpCompress.Compressors.Deflate;
19+
using SharpCompress.Writers.Zip;
1720

1821
namespace KKManager.ModpackTool
1922
{
@@ -131,35 +134,23 @@ private static async Task ProcessingThread()
131134
manifestWorkCopy.Save(writer, SaveOptions.OmitDuplicateNamespaces);
132135

133136
// ------ Recompress the archive
134-
//using (var zf = ZipArchive.Create())
135-
//using (var writeStream = File.OpenWrite(outPath))
136-
//{
137-
// zf.DeflateCompressionLevel = CompressionLevel.None;
138-
// zf.AddAllFromDirectory(tempDir);
139-
// zf.SaveTo(writeStream, new ZipWriterOptions(CompressionType.None)
140-
// {
141-
// //ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8),
142-
// //ArchiveComment = archiveComment
143-
// });
144-
//}
145-
146-
using (var writeStream = File.OpenWrite(outPath))
147-
using (var zf = new Ionic.Zip.ZipFile())
137+
using (var zf = ZipArchive.Create())
138+
using (var fs = File.Create(outPath))
148139
{
149-
zf.CompressionMethod = CompressionMethod.None;
150-
zf.CompressionLevel = CompressionLevel.None;
151-
zf.Comment = archiveComment;
152-
153-
zf.AddDirectory(tempDir, "");
154-
zf.Save(writeStream);
140+
zf.DeflateCompressionLevel = CompressionLevel.None;
141+
zf.AddAllFromDirectory(tempDir);
142+
zf.SaveTo(fs, new ZipWriterOptions(CompressionType.Deflate)
143+
{
144+
ArchiveComment = archiveComment,
145+
ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8),
146+
// 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???
147+
CompressionType = CompressionType.Deflate,
148+
DeflateCompressionLevel = CompressionLevel.None,
149+
UseZip64 = false,
150+
LeaveStreamOpen = false
151+
});
155152
}
156153

157-
158-
159-
160-
161-
162-
163154
// ------ Clean up
164155
await new DirectoryInfo(tempDir).SafeDelete();
165156

src/KKManager/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<package id="Ben.Demystifier" version="0.4.1" targetFramework="net472" />
44
<package id="DockPanelSuite" version="3.1.1" targetFramework="net472" />
55
<package id="DockPanelSuite.ThemeVS2015" version="3.1.1" targetFramework="net472" />
6-
<package id="DotNetZip" version="1.16.0" targetFramework="net472" />
76
<package id="MessagePack" version="2.5.192" targetFramework="net472" />
87
<package id="MessagePack.Annotations" version="2.5.192" targetFramework="net472" />
98
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.0" targetFramework="net472" />

0 commit comments

Comments
 (0)