复合文档,ISrorage

复合文档的格式详见:http://guooscar.iteye.com/blog/365356

复 合文档简单的说就是在一个文件里可以内嵌其他各种文档,这些内嵌的文档还具有目录结构,可以说复合文档格式就是在一个文件里面实现一个文件系统。从逻辑上 说复合文档包含Storage和Stream。Storge相当于我们熟知的操作系统文件系统里的目录,Stream相当于文件。下图就是复合文档的逻辑 示意图。和操作系统的文件系统类似,同一个Storge下的Stream不可以重名,不同Storage下的则可以。

复合文档就有另一个概念了,目录(Directory)。Directory包含一个描述Stream和Storge的元 数据的数据结构。如下图所示,目录里面有目录项,目录项也有编号,从0开始。每 个目录项就描述该项是Stream还是Storge,或者其他。如果是Stream,那么就记录起始扇区号是多少,如果是Storge,包含记录它有哪些 Storge和Stream。一个Storge下可能有很多个Storge和Stream。复合文档是把这些子Stream和Storge组成一棵红黑 树。这样Storge就可以只记录这颗红黑树的根节点就达到记录所有子Storge和Stream的目的。当然每个目录项得有字段里维护这么一颗红黑树。 显然这种方式下,一个Stream或Storge只能在一棵红黑树里。为啥使用红黑树,有什么优点?慢慢思考中!
此外,复合文档还有其他一些概念,比如Short-Stream。Short-Stream一般存储在Root Storge这个目录项里。

 

The IStorage interface supports the creation and management of structured storage objects. Structured storage allows hierarchical storage of information within a single file, and is often referred to as "a file system within a file". Elements of a structured storage object are storages and streams. Storages are analogous to directories, and streams are analogous to files. Within a structured storage there will be a primary storage object that may contain substorages, possibly nested, and streams. Storages provide the structure of the object, and streams contain the data, which is manipulated through the IStream interface.

The IStorage interface provides methods for creating and managing the root storage object, child storage objects, and stream objects. These methods can create, open, enumerate, move, copy, rename, or delete the elements in the storage object.

The IStream interface lets you read and write data to stream objects. Stream objects contain the data in a structured storage object, where storages provide the structure. Simple data can be written directly to a stream but, most frequently, streams are elements nested within a storage object. They are similar to standard files.

The IStream interface defines methods similar to the MS-DOS FAT file functions. For example, each stream object has its own access rights and a seek pointer. The main difference between a DOS file and a stream object is that in the latter case, streams are opened using an IStream interface pointer rather than a file handle.

The methods in this interface present your object's data as a contiguous sequence of bytes that you can read or write. There are also methods for committing and reverting changes on streams that are open in transacted mode and methods for restricting access to a range of bytes in the stream.

一些例子:http://www.360doc.com/content/07/0104/21/16867_318630.shtml

一、复合文件的特点
 
  1. 复合文件的内部是使用指针构造的一棵树进行管理的。编写程序的时候要注意,由于使用的是单向指针,因此当做定位操作的时候,向后定位比向前定位要快;
  2. 复合文件中的“流对象”,是真正保存数据的空间。它的存储单位为512字节。也就是说,即使你在流中只保存了一个字节的数据,它也要占据512字节的文件空间。
  3. 不同的进程,或同一个进程的不同线程可以同时访问一个复合文件的不同部分而互不干扰;
  4. 大家都有这样的体会,当需要往一个文件中插入一个字节的话,需要对整个文件进行操作,非常烦琐并且效率低下。而复合文件则提供了非常方便的“增量访问”能力;
  5. 当频繁地删除文件,复制文件后,磁盘空间会变的很零碎,需要使用磁盘整理工具进行重新整合。和磁盘管理非常相似,复合文件也会产生这个问题,在适当的时候也需要整理,但比较简单,只要调用一个函数就可以完成了。
二、持续性原理
    持续性,也叫永久性。组件方提供 IPersistXXX 接口,调用者(容器)提供存储介质,比如文件啦、内存啦、注册表啦、流啦、文本啦......啦啦拉。需要保存的时候,调用者通过 IPersistXXX::Save() 接口函数让组件去自己存储属性信息,而调用者根本不用关心存储格式和存储内容;需要还原状态的时候,调用者打开存储介质,然后同样调用 IPersistXXX::Load() 接口函数让组件自己去读取属性信息并完成初始化的设置。
posted @ 2013-06-25 14:10  Himer  阅读(728)  评论(0编辑  收藏  举报