我们知道,复合文件是文件系统的的文件系统,在结构化存储当中复合文件是不能避免的,目录就是相当于IStorage,文件呢则是IStream,在一个当前目录中,深入内部进行查找,如果是目录呢,则创建IStorage对象,对象名称是目录名称,如果是文件呢,则读取文件之后呢,将文件的内容读入到内存,然后写入流中

void main()
{
::CoInitialize(NULL);
wchar_t* szPath=L"D:\\okb\\*.*";
LPSTORAGE stg=NULL;
HRESULT hResult=::StgCreateDocfile(L"D:\\tmp.kil",STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE,0,&stg);
if(S_OK!=hResult)
{
MessageBox(L"创建符合文件失败",0,0);
return;
}
MainProc(szPath,stg);

::CoUninitialize();
}
void MainProc(wchar_t*szPath,LPSTORAGE& stg)
{
WIN32_FIND_DATA data;
HANDLE hFind=::FindFirstFile (szPath,&data);
if(INVALID_HANDLE_VALUE==hFind)
{
MessageBox(L"查找文件失败",0,0);
}
do
{
//只查找txt文件和目录文件
if(wcscmp(data.cFileName,L".")==0)
{
continue;
}else if(wcscmp(data.cFileName,L"..")==0)
{
continue;
}else if(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
IStorage*pSubstg=NULL;
stg->CreateStorage(data.cFileName,STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE,0,0,&pSubstg);
wchar_t Path[1024]={0};
wcscpy(Path,szPath);
wcscpy(&Path[wcslen(szPath)-3],data.cFileName);
wcscpy(Path+wcslen(Path),L"\\*.*");
MainProc(Path,pSubstg);

}else
{
//首先假定全是txt文档
IStream* psubStm=NULL;
stg->CreateStream(data.cFileName,STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,0,0,&psubStm);
//构造文件名称
wchar_t Path[1024]={0};
wcscpy(Path,szPath);
wcscpy(&Path[wcslen(szPath)-3],data.cFileName);
char *name=NULL;
DWORD n=WideCharToMultiByte(CP_OEMCP,NULL,Path,-1,NULL,0,NULL,FALSE);
name=new char[n];
WideCharToMultiByte(CP_OEMCP,NULL,Path,-1,name,n,NULL,FALSE);
ifstream input;
input.open(name);
int filesize=data.nFileSizeLow;
char* buf=new char[filesize+1];
memset(buf,0,filesize+1);
input.read(buf,filesize);
psubStm->Write(buf,filesize,NULL);
delete [] name;
delete [] buf;
buf=NULL;
name=NULL;
psubStm->Release();


}
}while(FindNextFile(hFind,&data));
stg->Release();
}

我之前试过了使用CFileFind可是总是找不到相应的文件,很是苦恼,于是换了这种方式,效果还行,类似于COM原理与应用的结构化存储实验