internal CompoundDocument(byte[] doc)
        {
            ILockBytes lb;
            var iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb);

            IntPtr buffer = Marshal.AllocHGlobal(doc.Length);
            Marshal.Copy(doc, 0, buffer, doc.Length);
            UIntPtr readSize;
            lb.WriteAt(0, buffer, doc.Length, out readSize);
            Marshal.FreeHGlobal(buffer);

            if (StgIsStorageILockBytes(lb) == 0)
            {
                IStorage storage = null;
                if (StgOpenStorageOnILockBytes(
                    lb,
                    null,
                    STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
                    IntPtr.Zero,
                    0,
                    out storage) == 0)
                {
                    Storage = new StoragePart();
                    ReadParts(storage, Storage);
                    Marshal.ReleaseComObject(storage);
                }
            }
            else
            {
                throw (new InvalidDataException(string.Format("Part is not a compound document")));
            }
        }
示例#2
0
        internal CompoundDocument(byte[] doc)
        {
            ILockBytes lb;
            var        iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb);

            IntPtr buffer = Marshal.AllocHGlobal(doc.Length);

            Marshal.Copy(doc, 0, buffer, doc.Length);
            UIntPtr readSize;

            lb.WriteAt(0, buffer, doc.Length, out readSize);
            Marshal.FreeHGlobal(buffer);

            if (StgIsStorageILockBytes(lb) == 0)
            {
                IStorage storage = null;
                if (StgOpenStorageOnILockBytes(
                        lb,
                        null,
                        STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
                        IntPtr.Zero,
                        0,
                        out storage) == 0)
                {
                    Storage = new StoragePart();
                    ReadParts(storage, Storage);
                    Marshal.ReleaseComObject(storage);
                }
            }
            else
            {
                throw (new InvalidDataException(string.Format("Part is not a compound document")));
            }
        }
示例#3
0
 internal void Read(FileInfo fi)
 {
     using (var doc = new CompoundDocumentFile(fi, tempFolder))
     {
         Storage = new StoragePart();
         GetStorageAndStreams(Storage, doc.RootItem);
     }
 }
示例#4
0
 internal void Read(MemoryStream ms)
 {
     using (var doc = new CompoundDocumentFile(ms))
     {
         Storage = new StoragePart();
         GetStorageAndStreams(Storage, doc.RootItem);
     }
 }
示例#5
0
 private void CreateStreams(StoragePart subStore, IStorage subStorage)
 {
     foreach (var ds in subStore.DataStreams)
     {
         comTypes.IStream stream;
         subStorage.CreateStream(ds.Key, (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), 0, 0, out stream);
         stream.Write(ds.Value, ds.Value.Length, IntPtr.Zero);
     }
     subStorage.Commit(0);
 }
示例#6
0
        private void CreateStore(string name, StoragePart subStore, IStorage storage)
        {
            IStorage subStorage;

            storage.CreateStorage(name, (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), 0, 0, out subStorage);
            storage.Commit(0);
            foreach (var store in subStore.SubStorage)
            {
                CreateStore(store.Key, store.Value, subStorage);
            }

            CreateStreams(subStore, subStorage);
        }
示例#7
0
 private void GetStorageAndStreams(StoragePart storage, CompoundDocumentItem parent)
 {
     foreach (var item in parent.Children)
     {
         if (item.ObjectType == 1)      //Substorage
         {
             var part = new StoragePart();
             storage.SubStorage.Add(item.Name, part);
             GetStorageAndStreams(part, item);
         }
         else if (item.ObjectType == 2) //Stream
         {
             storage.DataStreams.Add(item.Name, item.Stream);
         }
     }
 }
示例#8
0
        private MemoryStream ReadParts(IStorage storage, StoragePart storagePart)
        {
            MemoryStream ret = null;

            comTypes.STATSTG statstg;

            storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT);

            IEnumSTATSTG pIEnumStatStg = null;

            storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg);

            comTypes.STATSTG[] regelt = { statstg };
            uint fetched = 0;
            uint res     = pIEnumStatStg.Next(1, regelt, out fetched);

            //if (regelt[0].pwcsName == "DataSpaces")
            //{
            //    PrintStorage(storage, regelt[0],"");
            //}
            while (res != 1)
            {
                foreach (var item in regelt)
                {
                    if (item.type == 1)
                    {
                        IStorage subStorage;
                        storage.OpenStorage(item.pwcsName, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out subStorage);
                        StoragePart subStoragePart = new StoragePart();
                        storagePart.SubStorage.Add(item.pwcsName, subStoragePart);
                        ReadParts(subStorage, subStoragePart);
                    }
                    else
                    {
                        storagePart.DataStreams.Add(item.pwcsName, GetOleStream(storage, item));
                    }
                }
                res = pIEnumStatStg.Next(1, regelt, out fetched);
            }
            Marshal.ReleaseComObject(pIEnumStatStg);
            return(ret);
        }
示例#9
0
 private void WriteStorageAndStreams(StoragePart storage, CompoundDocumentItem parent)
 {
     foreach (var item in storage.SubStorage)
     {
         var c = new CompoundDocumentItem()
         {
             Name = item.Key, ObjectType = 1, Stream = null, StreamSize = 0, Parent = parent
         };
         parent.Children.Add(c);
         WriteStorageAndStreams(item.Value, c);
     }
     foreach (var item in storage.DataStreams)
     {
         var c = new CompoundDocumentItem()
         {
             Name = item.Key, ObjectType = 2, Stream = item.Value, StreamSize = (item.Value == null ? 0 : item.Value.Length), Parent = parent
         };
         parent.Children.Add(c);
     }
 }
示例#10
0
 internal void Read(ILockBytes lb)
 {
     if (StgIsStorageILockBytes(lb) == 0)
     {
         IStorage storage = null;
         if (StgOpenStorageOnILockBytes(
                 lb,
                 null,
                 STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
                 IntPtr.Zero,
                 0,
                 out storage) == 0)
         {
             Storage = new StoragePart();
             ReadParts(storage, Storage);
             Marshal.ReleaseComObject(storage);
         }
     }
     else
     {
         throw (new InvalidDataException(string.Format("Part is not a compound document")));
     }
 }
示例#11
0
 private void CreateStore(string name, StoragePart subStore, IStorage storage)
 {
     IStorage subStorage;
     storage.CreateStorage(name, (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), 0, 0, out subStorage);
     storage.Commit(0);
     foreach (var store in subStore.SubStorage)
     {
         CreateStore(store.Key, store.Value, subStorage);
     }
     
     CreateStreams(subStore, subStorage);
 }
示例#12
0
        private MemoryStream ReadParts(IStorage storage, StoragePart storagePart)
        {
            MemoryStream ret = null;
            comTypes.STATSTG statstg;

            storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT);

            IEnumSTATSTG pIEnumStatStg = null;
            storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg);

            comTypes.STATSTG[] regelt = { statstg };
            uint fetched = 0;
            uint res = pIEnumStatStg.Next(1, regelt, out fetched);

            //if (regelt[0].pwcsName == "DataSpaces")
            //{
            //    PrintStorage(storage, regelt[0],"");
            //}
            while (res != 1)
            {
                foreach (var item in regelt)
                {
                    if (item.type == 1)
                    {
                        IStorage subStorage;
                        storage.OpenStorage(item.pwcsName, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out subStorage);
                        StoragePart subStoragePart=new StoragePart();
                        storagePart.SubStorage.Add(item.pwcsName, subStoragePart);
                        ReadParts(subStorage, subStoragePart);
                    }
                    else
                    {
                        storagePart.DataStreams.Add(item.pwcsName, GetOleStream(storage, item));                    
                    }
                }
                res = pIEnumStatStg.Next(1, regelt, out fetched);
            }
            Marshal.ReleaseComObject(pIEnumStatStg);
            return ret;
        }
示例#13
0
 internal CompoundDocument()
 {
     Storage = new CompoundDocument.StoragePart();
 }
示例#14
0
 internal CompoundDocument()
 {
     Storage = new CompoundDocument.StoragePart();
 }
示例#15
0
 private void CreateStreams(StoragePart subStore, IStorage subStorage)
 {
     foreach (var ds in subStore.DataStreams)
     {
         comTypes.IStream stream;
         subStorage.CreateStream(ds.Key, (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), 0, 0, out stream);
         stream.Write(ds.Value, ds.Value.Length, IntPtr.Zero);
     }
     subStorage.Commit(0);
 }
示例#16
0
 internal void Read(ILockBytes lb)
 {
     if (StgIsStorageILockBytes(lb) == 0)
     {
         IStorage storage = null;
         if (StgOpenStorageOnILockBytes(
             lb,
             null,
             STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
             IntPtr.Zero,
             0,
             out storage) == 0)
         {
             Storage = new StoragePart();
             ReadParts(storage, Storage);
             Marshal.ReleaseComObject(storage);
         }
     }
     else
     {
         throw (new InvalidDataException(string.Format("Part is not a compound document")));
     }
 }
示例#17
0
 internal CompoundDocument(string tempFolder)
 {
     this.tempFolder = tempFolder;
     Storage         = new CompoundDocument.StoragePart();
 }