Search code examples
c#compressionembedded-resourceopenxml-sdk

Embed OpenXML SDK Files In .NET Executable


The Microsoft OpenXML SDK version 2.0 ships with a ~5111 KiB .NET assembly (DLL), and a ~14436 KiB uncompressed XML file. Both of these are required to successfully use the SDK. (Important update: Only the DLL file is required; the XML is just API documentation for the programmer!!)

I've written a program using the SDK that works and I'm satisfied with it. However, I compressed the OpenXML SDK data (DLL and XML) using LZMA2, and the result is only 1287 KiB. The savings is 18260 KiB, which can take at least a few seconds, up to several minutes, to transfer over an average WAN link.

I downloaded the LZMA SDK from 7-zip.org which implements LZMA compression and decompression in about 45 KiB of compiled MSIL (as compiled by .NET Framework 4.0).

My goal is to compress the resources (using something like 7-Zip), store them in the built assembly (.exe), then decompress them and load them. I know how to use the 7-Zip SDK to do decompression. I have two problems thus:

  1. How do I load the OpenXML SDK assembly (.dll) into the running .NET process without writing it to disk? Assume I have a byte[] of the uncompressed bytes of the DLL; I'll take care of resource packing into the executable by myself.

  2. How do I tell the assembly to load the OpenXML schema (DocumentFormat.OpenXml.xml) from memory without writing it to disk? Assume I have a byte[] or string containing the uncompressed XML. Again, I will take care of decompression and resource packing myself.


Solution

  • Use the appropriate overload of Assembly.Load after decompressing to byte[]. The DocumentFormat.OpenXML.xml file is not required for an application to execute. It is just API documentation for developers. It gets used by Visual Studio to show information in Intellisense.

    You should make the dll (or compressed dll) an embedded resource and the use AppDomain.AssemblyResolve event to load the SDK when it requested. There is some sample code of that here.