Skip to content

Commit

Permalink
Compress the resources, reducing DLL size from 16MB to 5MB.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Oct 18, 2023
1 parent 27271a5 commit d174dc7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Segmenter/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Reflection;

namespace JiebaNet.Segmenter
Expand All @@ -26,7 +27,9 @@ public class ConfigManager

private static T ReadFile<T>(string name, Func<Stream, T> read)
{
using var stream = typeof(ConfigManager).Assembly.GetManifestResourceStream($"JiebaNet.Segmenter.Resources.{name}");
using var zipStream = typeof(ConfigManager).Assembly.GetManifestResourceStream("Resources.zip") ?? throw new InvalidOperationException("Missing Resources.zip.");
using var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
using var stream = zipArchive.GetEntry(name)?.Open() ?? throw new InvalidOperationException($"Missing {name} from Resources.zip.");
return read(stream);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Segmenter/Segmenter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
</AssemblyAttribute>
</ItemGroup>

<Target Name="BuildResourcesZip" BeforeTargets="BeforeBuild">
<ZipDirectory SourceDirectory="Resources" DestinationFile="obj\$(Configuration)\$(TargetFramework)\Resources.zip" Overwrite="true" />
</Target>

<ItemGroup>
<EmbeddedResource Include="Resources\*" />
<EmbeddedResource Include="obj\$(Configuration)\$(TargetFramework)\Resources.zip" LogicalName="Resources.zip" Visible="false" />
</ItemGroup>

</Project>

0 comments on commit d174dc7

Please sign in to comment.