1

I'm working on a project where I need to edit the contents of existing office files. I'm using the open source library OpenMCDF (https://sourceforge.net/p/openmcdf) which provides easy access to access the data in compound ole files. I've found it excellent for reading data but am having problems writing to them.

I've written a small code sample to demonstrate the problem as per below (note you will need to change the file path). In this case I take a particular stream (./_VBA_PROJECT/VBA/dir) and re-write the contents of the stream truncating the stream by 50 bytes. To demonstrate the corruption, if you open the output file using 7zip and try to export the dir stream you are informed that the dir stream is broken.

The problem only seems to be a problem when you write a stream shorter than the original. Adding more bytes doesn't seem to cause a problem.

Any help here would be much appreciated.

using OpenMcdf;


namespace OpenMcdfTest
{
    class Program
    {
        static void Main(string[] args)
        {
            const string FILE_PATH = @"c:\users\ross\desktop\temp.xls";

            CompoundFile cf = new CompoundFile(FILE_PATH);

            CFStream dirStream = cf.RootStorage.GetStorage("_VBA_PROJECT_CUR").GetStorage("VBA").GetStream("dir");

            byte[] currentData = dirStream.GetData();

            Array.Resize(ref currentData, currentData.Length - 50);

            dirStream.SetData(currentData);

            cf.Save(FILE_PATH + ".edited");
            cf.Close();
        }
    }
}
Community
  • 1
  • 1
Rossco
  • 1,052
  • 11
  • 24
  • Ok, I haven't found the answer to my question fully yet but here is some things I have discovered. Deleting a stream and then adding it again seems to overcome the corruption issues. This is a workaround for the moment. – Rossco Jan 28 '14 at 19:56
  • Also I have cloned the project from SourceForge to GitHub (https://github.com/rossknudsen/OpenMCDF). Upgraded the assemblies to VS 2012, added strong naming to the assemblies. I hope to wrap it all up in a NuGet package and publish (once I figure out how to do that). – Rossco Jan 28 '14 at 19:59

0 Answers0