Introduction
I have been looking for a compression library that can compress XML fragments
on the fly from the IStream
interface and decompress it back. There
are several file compression libraries, most based on zlib
, but I needed custom storage such as compressed streams inside structured
storage files.
This is a first shot at wrapping zlib
compression library behind a COM IStream
interface. I provide a
pure ATL C++ implementation and a simple demo COM based implementation. Both
take an IStream
as an initializer and compress/decompress on the
fly on the Read
and Write
methods.
Using the code
I hope this will be usefull for more people than me. I haven't tried this
with MSXML SAX Parser yet but as I understand it the SAX Parser should bot need
more than the ISequentialStream interface and thus should work fine with this
implementation.
These are the classes found in the header file zlibistream.h
The following snippets shows how to use the C++ wrapper on an IStream
CComPtr<IStream> spStm;
...
ZWriteSequentialStream zw;
zw.Initialize(spStm);
zw.Write((void*)buffer, bufLen, &cb);
zw.Cleanup();
CComPtr<IStream> spStm;
...
ZReadSequentialStream zr;
zr.Initialize(spStm);
zr.Read((void*)buffer, bufLen, &cb);
zr.Cleanup();
The following snippets shows how to use the COM wrapper on an IStream
CComPtr<IStream> spStm;
...
CComPtr<IZWriteSeqStream> spWrite;
if (SUCCEEDED(spWrite.CoCreateInstance(__uuidof(ZWriteSeqStream))))
{
spWrite->Initialize(CComVariant(spStm));
CComQIPtr<ISequentialStream> spSeq = spWrite;
spSeq->Write((void*)buffer, bufLen, &cb);
spWrite.Release();
}
CComPtr<IStream> spStm;
...
CComPtr<IZReadSeqStream> spRead;
if (SUCCEEDED(spRead.CoCreateInstance(__uuidof(ZReadSeqStream))))
{
spRead->Initialize(CComVariant(spStm));
CComQIPtr<ISequentialStream> spSeq = spRead;
spSeq->Read((void*)buffer, bufLen, &cb);
spRead.Release();
}
Points of Interest
The only problem, so far, was to synchronize the zstream
buffer
in zlib so the data is available for the
Read
and Write
IStream
functions.
The demo project includes a static library for zlib 1.1.4
Links
zlib web site
History
2003.01.24 First implementation