📄 zstream.h
字号:
// Zstream.h: interface for the CZstream class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_ZSTREAM_H__8947C480_A354_4875_9D09_DFDBCF38607C__INCLUDED_)
#define AFX_ZSTREAM_H__8947C480_A354_4875_9D09_DFDBCF38607C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <zlib.h>
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <stdio.h>
int write_stream(LPCSTR sName,void *pBuffer,int iSize);
CComPtr<IStream> read_stream(LPCSTR sName,DWORD iSize);
int open_StgDoc(LPCSTR fileName);
int close_StgDoc();
int stream2file(LPCSTR sName,LPCSTR fName,int iSize);
class CStructureStorage
{
public:
CComPtr<IStorage> m_spStorage;
CStructureStorage() {}
CStructureStorage(LPCSTR lpszFileName, bool bForEdit = false)
{
Open(lpszFileName, bForEdit);
}
CStructureStorage(LPCWSTR lpszFileName, bool bForEdit = false)
{
Open(lpszFileName, bForEdit);
}
~CStructureStorage() {}
bool Open(LPCSTR lpszFileName, bool bForEdit = false)
{
USES_CONVERSION;
return Open(T2CW(lpszFileName), bForEdit);
}
bool Open(LPCWSTR lpszFileName, bool bForEdit = false)
{
DWORD grfMode = 0;
if (bForEdit)
grfMode = STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
else
grfMode = STGM_READ | STGM_SHARE_DENY_WRITE;
CComPtr<IStorage> pStg;
HRESULT hr = ::StgOpenStorage(lpszFileName, NULL, grfMode, NULL, 0, &pStg);
if (FAILED(hr) && bForEdit)
{
grfMode |= STGM_CREATE;
hr = ::StgCreateDocfile(lpszFileName, grfMode, 0, &pStg);
}
if (SUCCEEDED(hr))
m_spStorage = pStg;
ATLASSERT(m_spStorage != NULL);
return SUCCEEDED(hr);
}
CComPtr<IStream> OpenStream(LPCSTR lpszStreamName, bool bForEdit = false)
{
USES_CONVERSION;
return OpenStream(T2CW(lpszStreamName), bForEdit);
}
CComPtr<IStream> OpenStream(LPCWSTR lpszStreamName, bool bForEdit = false)
{
HRESULT hr = S_OK;
CComPtr<IStream> spStm;
DWORD grfMode = STGM_SHARE_EXCLUSIVE;
if (bForEdit)
{
grfMode |= STGM_READWRITE | STGM_CREATE;
hr = m_spStorage->CreateStream(lpszStreamName, grfMode, 0, 0, &spStm);
}
else
{
grfMode |= STGM_READ;
hr = m_spStorage->OpenStream(lpszStreamName, NULL, grfMode, 0, &spStm);
}
return spStm;
}
void Commit()
{
if (m_spStorage)
m_spStorage->Commit(STGC_DEFAULT);
}
bool Close()
{
m_spStorage.Release();
return true;
}
};
class ZReadStream
{
public:
CComPtr<IStream> m_spStm;
bool m_bInitialized;
STDMETHOD(Read)(IStream * pStream,DWORD iSize)
{
HRESULT hr = S_OK;
STATSTG stg;
m_spStm->Stat(&stg,0);
ULARGE_INTEGER uSize=stg.cbSize;
ULONG zSize=(ULONG)uSize.QuadPart;
byte * zBuf=(byte *)malloc(zSize);//要求支持64位整数
byte * pBuf=(byte *)malloc(iSize);//要求支持64位整数
ULONG pcbRead=0;
ULONG pcbWrite=0;
m_spStm->Read(zBuf,zSize, &pcbRead);
int rst=uncompress(pBuf,&iSize,zBuf,pcbRead);
pStream->Write(pBuf,iSize,&pcbWrite);
free(zBuf);
free(pBuf);
return hr;
}
HRESULT FinalConstruct() { return S_OK; }
void FinalRelease() { Cleanup(); }
// Initializer and cleanup
bool Initialize(IStream* p)
{
m_bInitialized = true;
m_spStm = p;
return true;
}
bool Cleanup()
{
if (!m_bInitialized)
return true;
m_bInitialized = false;
m_spStm.Release();
return true;
}
// overrides
HRESULT FillBuffer(void *pv, ULONG cb, ULONG *pcbRead)
{
ATLASSERT(m_spStm != NULL);
if (!m_spStm)
{
if (pcbRead)
*pcbRead = 0;
return E_FAIL;
}
return m_spStm->Read(pv, cb, pcbRead);
}
};
class ZWriteStream
{
public:
ULONG m_streamSize;
CComPtr<IStream> m_spStm;
bool m_bInitialized;
STDMETHOD(Write)(void const *pv, ULONG cb, ULONG *pcbWritten)
{
HRESULT hr = S_OK;
uLong dLeng=(uLong)(cb+12);
byte * zBuf;
zBuf=(byte *)malloc(dLeng);
compress (zBuf, &dLeng, (Bytef*) pv, (uLong)cb);
ULONG zcb;
EmptyBuffer(zBuf, dLeng, &zcb);
m_streamSize=dLeng;
free(zBuf);
return hr;
}
bool Initialize(IStream* p)
{
m_bInitialized = true;
m_spStm = p;
return true;
}
bool Cleanup()
{
if (!m_bInitialized)
return true;
m_bInitialized = false;
if (m_spStm)
{
ULARGE_INTEGER ul;
ul.QuadPart = __int64(m_streamSize);
m_spStm->SetSize(ul);
m_spStm->Commit(STGC_DEFAULT);
}
m_spStm.Release();
return true;
}
// overrides
HRESULT EmptyBuffer(void const *pv, ULONG cb, ULONG *pcbWritten)
{
ATLASSERT(m_spStm != NULL);
if (!m_spStm)
{
if (pcbWritten)
*pcbWritten = 0;
return E_FAIL;
}
return m_spStm->Write(pv, cb, pcbWritten);
}
};
#endif // !defined(AFX_ZSTREAM_H__8947C480_A354_4875_9D09_DFDBCF38607C__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -