Skip to content

Commit

Permalink
Implement embedded resource decompressor
Browse files Browse the repository at this point in the history
  • Loading branch information
y0ung3r committed Apr 5, 2024
1 parent f8b5114 commit 485ff8b
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
Expand All @@ -19,4 +20,12 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Embedded\TestEmbeddedResource.xml.gz" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OfficeFlow.Word.OpenXml\OfficeFlow.Word.OpenXml.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Xml.Linq;
using FluentAssertions;
using OfficeFlow.Word.OpenXml.Resources.Embedded;
using OfficeFlow.Word.OpenXml.Resources.Exceptions;
using Xunit;

namespace OfficeFlow.Word.OpenXml.Tests.Resources.Embedded
{
public sealed class EmbeddedResourceDecompressorTests
{
[Fact]
public void Should_decompress_exists_embedded_resource_from_calling_assembly()
{
// Arrange
var expectedXml = new XElement
(
"document",
new XElement
(
"body",
new XText("Content")
)
);

var sut = new EmbeddedResourceDecompressor();

// Act
var actualXml = sut.Decompress("TestEmbeddedResource.xml.gz");

// Assert
actualXml
.Root
.Should()
.BeEquivalentTo(expectedXml);
}

[Fact]
public void Should_throws_exception_if_resource_not_found_in_calling_assembly()
=> new EmbeddedResourceDecompressor()
.Invoking(decompressor => decompressor.Decompress("NotExistsResource.xml.gz"))
.Should()
.Throw<ResourceNotFoundException>();
}
}
Binary file not shown.
11 changes: 10 additions & 1 deletion OfficeFlow.Word.OpenXml/OfficeFlow.Word.OpenXml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Packaging\" />
<PackageReference Include="System.IO.Packaging" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Embedded\Default\bulletNumbering.xml.gz" />
<EmbeddedResource Include="Resources\Embedded\Default\decimalNumbering.xml.gz" />
<EmbeddedResource Include="Resources\Embedded\Default\numbering.xml.gz" />
<EmbeddedResource Include="Resources\Embedded\Default\styles.xml.gz" />
<EmbeddedResource Include="Resources\Embedded\Default\wordDocument.xml.gz" />
<EmbeddedResource Include="Resources\Embedded\Default\wordStyles.xml.gz" />
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using OfficeFlow.Word.OpenXml.Resources.Exceptions;
using OfficeFlow.Word.OpenXml.Resources.Interfaces;

namespace OfficeFlow.Word.OpenXml.Resources.Embedded
{
public sealed class EmbeddedResourceDecompressor : IResourceDecompressor
{
private readonly Assembly _assembly;

private static readonly Lazy<EmbeddedResourceDecompressor> InstanceFactory =
new Lazy<EmbeddedResourceDecompressor>(() => new EmbeddedResourceDecompressor());

public static EmbeddedResourceDecompressor Instance
=> InstanceFactory.Value;

public EmbeddedResourceDecompressor()
: this(Assembly.GetCallingAssembly())
{ }

public EmbeddedResourceDecompressor(Assembly assembly)
=> _assembly = assembly;

private Stream GetResourceStream(string resourceName)
{
var resourcePath = _assembly
.GetManifestResourceNames()
.FirstOrDefault(name => name.EndsWith(resourceName))
?? throw new ResourceNotFoundException(resourceName);

return _assembly.GetManifestResourceStream(resourcePath)
?? throw new ResourceNotLoadedException(resourceName);
}

public XDocument Decompress(string resourceName)
{
var stream = GetResourceStream(resourceName);
using var zip = new GZipStream(stream, CompressionMode.Decompress);
using var reader = new StreamReader(zip);

return XDocument.Load(reader);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace OfficeFlow.Word.OpenXml.Resources.Exceptions
{
public sealed class ResourceNotFoundException : Exception
{
public ResourceNotFoundException(string resourceName, Exception? innerException = null)
: base($"Resource \"{resourceName}\" not found", innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace OfficeFlow.Word.OpenXml.Resources.Exceptions
{
public sealed class ResourceNotLoadedException : Exception
{
public ResourceNotLoadedException(string resourceName, Exception? innerException = null)
: base($"Failed to load resource \"{resourceName}\"", innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Xml.Linq;

namespace OfficeFlow.Word.OpenXml.Resources.Interfaces
{
public interface IResourceDecompressor
{
XDocument Decompress(string resourceName);
}
}

0 comments on commit 485ff8b

Please sign in to comment.