OpenMCDF is a library for .NET that allows us to work with Compound Document File (CDF) format files from an application written in C#.
OpenMCDF provides a way to read and write CDF files from applications written in C#. It allows operations such as extracting data from CDF files, modifying existing files, and creating new CDF files from scratch.
Some of its features are:
- Supports read/write operations on streams and storages.
- Allows traversing the file’s tree structure.
- Complies with CDF specifications versions 3 and 4.
- Uses lazy loading whenever possible to reduce memory usage.
- Offers an intuitive API for working with structured files.
CDF Files
CDF files, also known as OLE structured storage, are a binary file format used by many applications. For example:
- All documents created by Microsoft Office up to the 2007 version
- Outlook .msg messages
- Windows thumbnail cache files (thumbs.db)
- Visual Studio .suo files (solution options) are also compound files
- Many audio/video editing tools (*.aaf, for example).
CDF files are essentially containers that can store various types of data, such as text, images, embedded objects, metadata, and more. The advantage of CDF files is that they allow multiple data components to be stored and managed in a single file, making it easier to transport and exchange information more efficiently.
The hierarchical structure of a CDF file consists of two main elements:
Streams: These are individual data blocks containing specific information, such as text, images, embedded objects, etc. Each stream has a unique name that identifies it within the file.
Storages: These are containers that can hold other streams or storages. They allow organizing information in a tree structure. Storages can be nested, meaning a storage can contain other storages and streams.
How to Use OpenMCDF
We can add the library to a .NET project easily, through the corresponding Nuget package.
Install-Package OpenMCDF
Here are some examples of how to use OpenMCDF, extracted from the library’s documentation.
Create a New Compound File
byte[] b = new byte[10000];
CompoundFile cf = new CompoundFile();
CFStream myStream = cf.RootStorage.AddStream("MyStream");
myStream.SetData(b);
cf.Save("MyCompoundFile.cfs");
cf.Close();
Open an Existing File and Get a Data Stream
String filename = "report.xls";
CompoundFile cf = new CompoundFile(filename);
CFStream foundStream = cf.RootStorage.GetStream("Workbook");
byte[] temp = foundStream.GetData();
// Do something with 'temp'
cf.Close();
Add and Delete Items
CompoundFile cf = new CompoundFile();
CFStorage st = cf.RootStorage.AddStorage("MyStorage");
CFStream sm = st.AddStream("MyStream");
// Delete an item
cf.RootStorage.Delete("AStream"); // Assumes it exists.
Persist Changes
cf.RootStorage.AddStream("MyStream").SetData(buffer);
cf.Commit();
Compress a Compound File
CompoundFile.ShrinkCompoundFile("MultipleStorage_Deleted_Compress.cfs");
OpenMCDF is compatible with .NET Standard 2.0. It is an Open Source project, and all code and documentation is available in the repository at GitHub - ironfede/openmcdf

