Hi,
I'm having trouble with writing new data to existing compound files (office files). I've posted to StackOverflow as well (http://stackoverflow.com/questions/21269759/using-openmcdf-to-modify-xls-file-results-in-corrupted-output). Here is what I wrote there:
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();
}
}
}
Please, get latest version from dev branch (future 2.0 release) to verify bug closure.
Thank you very much for your report and contribution.
Federico
Hi Frederico,
Thanks for the update. By the way, I'm not sure if you noticed that I have actually forked your code to a GitHub repository (https://github.com/rossknudsen/OpenMCDF) and added a few things including a NuGet package definition and bug fixes. You are welcome to browse the code there and backport any changes to SF.
Alternatively if you would like to move to Github I can grant you full commit access.
Also, if you use StackOverflow, post your bugfix as an answer and when I test it I can mark it as an answer (which it is) and the question can be closed off.
Regards
Ross