Skip to content

Commit

Permalink
Upgraded to SharpZipLib
Browse files Browse the repository at this point in the history
The little remaining work is blocked until ParatextData updates their code to remove DotNetZip, as well
  • Loading branch information
josephmyers committed Jan 21, 2025
1 parent c5ca852 commit 49fb701
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/SIL.XForge.Scripture/SIL.XForge.Scripture.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="Duende.AccessTokenManagement" Version="3.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.7" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
Expand All @@ -41,6 +40,7 @@
InternetSettings.xml file. Also update server config scriptureforge.org_v2.yml. -->
<PackageReference Include="ParatextData" Version="9.5.0.8" />
<PackageReference Include="Serval.Client" Version="1.8.4" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.6.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
Expand Down
42 changes: 32 additions & 10 deletions src/SIL.XForge.Scripture/Services/SFInstallableDblResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Linq;
using System.Net;
using Ionic.Zip;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Paratext.Data;
Expand Down Expand Up @@ -400,9 +400,32 @@ public void ExtractToDirectory(string path)
);
if (RobustFile.Exists(resourceFile))
{
using var zipFile = ZipFile.Read(resourceFile);
using var stream = new FileStream(resourceFile, FileMode.Open);
using var zipFile = new ZipFile(stream);
zipFile.Password = this._passwordProvider?.GetPassword();
zipFile.ExtractAll(path, ExtractExistingFileAction.DoNotOverwrite);
ExtractAll(zipFile, path);
}
}

private static void ExtractAll(ZipFile zip, string path)
{
foreach (ZipEntry entry in zip)
{
if (!entry.IsFile)
continue; // Skip directories

string entryPath = Path.Combine(path, entry.Name);

if (File.Exists(entryPath))
continue; // Don't overwrite

// Ensure directories in the ZIP entry are created
Directory.CreateDirectory(Path.GetDirectoryName(entryPath));

// Extract the file
using Stream zipStream = zip.GetInputStream(entry);
using FileStream output = File.Create(entryPath);
zipStream.CopyTo(output);
}
}

Expand Down Expand Up @@ -510,9 +533,8 @@ internal static IReadOnlyDictionary<string, int> GetInstalledResourceRevisions()
// See if this a zip file, and if it contains the correct ID
try
{
// This only uses DotNetZip because ParatextData uses DotNetZip
// You could use System.IO.Compression if you wanted to
using var zipFile = ZipFile.Read(resourceFile);
using var stream = new FileStream(resourceFile, FileMode.Open);
using var zipFile = new ZipFile(stream);
// Zip files use forward slashes, even on Windows
const string idSearchPath = DblFolderName + "/id/";
const string revisionSearchPath = DblFolderName + "/revision/";
Expand All @@ -524,19 +546,19 @@ internal static IReadOnlyDictionary<string, int> GetInstalledResourceRevisions()
if (
string.IsNullOrWhiteSpace(fileId)
&& !entry.IsDirectory
&& entry.FileName.StartsWith(idSearchPath, StringComparison.OrdinalIgnoreCase)
&& entry.Name.StartsWith(idSearchPath, StringComparison.OrdinalIgnoreCase)
)
{
fileId = entry.FileName.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
fileId = entry.Name.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
}
else if (
revision == 0
&& !entry.IsDirectory
&& entry.FileName.StartsWith(revisionSearchPath, StringComparison.OrdinalIgnoreCase)
&& entry.Name.StartsWith(revisionSearchPath, StringComparison.OrdinalIgnoreCase)
)
{
string revisionFilename = entry
.FileName.Split('/', StringSplitOptions.RemoveEmptyEntries)
.Name.Split('/', StringSplitOptions.RemoveEmptyEntries)
.Last();
if (!int.TryParse(revisionFilename, out revision))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using Ionic.Zip;
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Paratext.Data;
using Paratext.Data.Languages;
using Paratext.Data.ProjectFileAccess;
Expand Down Expand Up @@ -38,7 +39,7 @@ public HexId CachedGuid
}
}

public ZipFile ZipFile { get; } = new ZipFile(new UTF8Encoding());
public ZipFile ZipFile { get; } = new ZipFile(new MemoryStream());

protected override ProjectFileManager CreateFileManager() =>
new MockZippedProjectFileManager(ZipFile, loadDblSettings: true, Name);

Check failure on line 45 in test/SIL.XForge.Scripture.Tests/Services/MockResourceScrText.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

Argument 1: cannot convert from 'ICSharpCode.SharpZipLib.Zip.ZipFile' to 'Ionic.Zip.ZipFile'

Check failure on line 45 in test/SIL.XForge.Scripture.Tests/Services/MockResourceScrText.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

Argument 1: cannot convert from 'ICSharpCode.SharpZipLib.Zip.ZipFile' to 'Ionic.Zip.ZipFile'

Check failure on line 45 in test/SIL.XForge.Scripture.Tests/Services/MockResourceScrText.cs

View workflow job for this annotation

GitHub Actions / Production build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

Argument 1: cannot convert from 'ICSharpCode.SharpZipLib.Zip.ZipFile' to 'Ionic.Zip.ZipFile'
Expand All @@ -48,7 +49,7 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
if (disposing)
{
ZipFile.Dispose();
(ZipFile as IDisposable).Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6786,7 +6786,8 @@ public MockResourceScrText GetResourceScrText(
CachedGuid = HexId.FromStr(projectId),
};
scrText.Settings.LanguageID = LanguageId.English;
scrText.ZipFile.AddFile(
scrText.ZipFile.BeginUpdate(); //todo check other places???
scrText.ZipFile.Add(
Path.Combine(ZippedProjectFileManagerBase.DBLFolderName, "language", "iso", zipLanguageCode)
);
return scrText;
Expand Down

0 comments on commit 49fb701

Please sign in to comment.