ironfede / openmcdf

Microsoft Compound File .net component - pure C# - netstandard 2.0
Mozilla Public License 2.0
302 stars 73 forks source link

Best way to copy a storage to another file #10

Closed Sicos1977 closed 6 years ago

Sicos1977 commented 7 years ago

Hi,

What is the best way to copy a CFStorage to another file. I tried it like this but for some reason I only end up with the streams in the CFStorage and not the CFStorage's in the source CFStorage

                case AttachmentType.ATTACH_EMBEDDED_MSG:
                    var msgStorage = storage.AddStorage(PropertyTags.PR_ATTACH_DATA_BIN.Name);
                    var cf = new CompoundFile(Stream);
                    Storage.Copy(cf.RootStorage, msgStorage);
                    propertiesStream.AddProperty(PropertyTags.PR_ATTACH_SIZE, Stream.Length);
                    break;

        #region Copy
        /// <summary>
        /// Copies the given <paramref name="source"/> to the given <paramref name="destination"/>
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void Copy(CFStorage source, CFStorage destination)
        {
            source.VisitEntries(action =>
            {
                if (action.IsStorage)
                {
                    var destionationStorage = destination.AddStorage(action.Name);
                    destionationStorage.CLSID = action.CLSID;
                    destionationStorage.CreationDate = action.CreationDate;
                    destionationStorage.ModifyDate = action.ModifyDate;
                    Copy(action as CFStorage, destionationStorage);
                }
                else
                {
                    var sourceStream = action as CFStream;
                    var destinationStream = destination.AddStream(action.Name);
                    if (sourceStream != null) destinationStream.SetData(sourceStream.GetData());
                }

            }, false);
        }
        #endregion
ironfede commented 6 years ago

I've checked your Copy method and it seems to work correctly.

public void Test_COPY_ENTRIES_FROM_TO_STORAGE()
{
    CompoundFile cfDst = new CompoundFile();
    CompoundFile cfSrc = new CompoundFile("MultipleStorage4.cfs");
    Copy(cfSrc.RootStorage, cfDst.RootStorage);
    cfDst.Save("MultipleStorage4Copy.cfs");

    cfDst.Close();
    cfSrc.Close();
}

Please, give me more details on this issue because I can't reproduce it.