Click here to Skip to main content
15,868,340 members
Articles / Desktop Programming / ATL
Article

Zlib compression / decompression wrapper as ISequentialStream

Rate me:
Please Sign up or sign in to vote.
4.17/5 (12 votes)
31 Jan 20031 min read 106.7K   3K   50   12
This article discusses wrapping compression and decompression behind a ISeqentialStream interface

Sample Image - zlibstream.jpg

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

//
// ZSequentialStreamBase , base class for the implementaion classes
//
// Implementation classes for ISequentialStream interfaces, constrained to
// either writing or reading. Template class T needs to provide implementaion
// for overridable member functions FillBuffer and EmptyBuffer.
//
// template<class T, int bufferSize=16384> class ZReadSequentialStreamImpl;
// template<class T, int bufferSize=16384> class ZWriteSequentialStreamImpl;
//
//
// Implementation of read and write sequential streams based on implementation
// classes above. These classe overrides the FillBuffer and EmptyBuffer by
// reading from or writing to a user supplied stream.
//
// template<int bufferSize=16384> class ZReadSequentialStreamT;
// template<int bufferSize=16384> class ZWriteSequentialStreamT;
//
// Convenience typedef's for stackbased classes (no AddRef(), Release() or QI)
//
// typedef CComObjectStack<ZWriteSequentialStreamT<> > ZWriteSequentialStream;
// typedef CComObjectStack<ZReadSequentialStreamT<> > ZReadSequentialStream;
//

The following snippets shows how to use the C++ wrapper on an IStream

// Writing to a stream
CComPtr<IStream> spStm;
... // get an IStream for writing to storage

// Use zlib sequential stream implementation for writing (compression)
ZWriteSequentialStream zw;
zw.Initialize(spStm); // initialize for writing to the IStream
zw.Write((void*)buffer, bufLen, &cb);
zw.Cleanup();


// Reading from a stream
CComPtr<IStream> spStm;
... // get an IStream for reading from storage

// Use zlib sequential stream implementation for reading (decompression)
ZReadSequentialStream zr;
zr.Initialize(spStm); // initialize for reading from the IStream
zr.Read((void*)buffer, bufLen, &cb);
zr.Cleanup();

The following snippets shows how to use the COM wrapper on an IStream

// Writing to a stream
CComPtr<IStream> spStm;
... // get an IStream for writing to storage

// Use COM wrapper for writing (compression)
CComPtr<IZWriteSeqStream> spWrite;
if (SUCCEEDED(spWrite.CoCreateInstance(__uuidof(ZWriteSeqStream))))
{
    spWrite->Initialize(CComVariant(spStm));
    CComQIPtr<ISequentialStream> spSeq = spWrite;
    spSeq->Write((void*)buffer, bufLen, &cb);

    //cleanup
    spWrite.Release();
}


// Reading from a stream
CComPtr<IStream> spStm;
... // get an IStream for reading from storage

// Use COM wrapper for reading (decompression)
CComPtr<IZReadSeqStream> spRead;
if (SUCCEEDED(spRead.CoCreateInstance(__uuidof(ZReadSeqStream))))
{
    spRead->Initialize(CComVariant(spStm));
    CComQIPtr<ISequentialStream> spSeq = spRead;
    spSeq->Read((void*)buffer, bufLen, &cb);

    //cleanup
    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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIZWriteSeqStream exists nowhere but in the text of this article Pin
BugMeNot ACCOUNT7-Mar-08 1:20
BugMeNot ACCOUNT7-Mar-08 1:20 
GeneralRe: IZWriteSeqStream exists nowhere but in the text of this article Pin
BugMeNot ACCOUNT7-Mar-08 2:48
BugMeNot ACCOUNT7-Mar-08 2:48 
GeneralBigger file handling. Pin
jagatnibas13-Nov-07 11:39
jagatnibas13-Nov-07 11:39 
QuestionHuge file / incremental support? Pin
NeverShaveYourDuck13-Aug-07 10:13
NeverShaveYourDuck13-Aug-07 10:13 
QuestionCan not compile "zlibstream_demo" in VS 2005 Pin
R_PR12-Jul-07 5:53
R_PR12-Jul-07 5:53 
QuestionNeed Help: Issue with ZlibCleanup Pin
Dennis Jiang12-Jan-07 22:03
Dennis Jiang12-Jan-07 22:03 
Generalgreat article! Pin
stefanandrei24-Nov-05 0:15
stefanandrei24-Nov-05 0:15 
GeneralIStream with gzFile Pin
Member 112381319-Jul-05 22:20
Member 112381319-Jul-05 22:20 
GeneralDemo project corrupted. Pin
TW4-Feb-03 23:06
TW4-Feb-03 23:06 
GeneralRe: Demo project corrupted. Pin
Jens Nilsson6-Feb-03 11:03
Jens Nilsson6-Feb-03 11:03 
Hi,

I saw that I used a hardcoded path in the demo executable. If you change the file path C:\work\source\zlibstream\test.stg to something more local and recompile or make sure the path above exisits, then all should be fine.

If you're running into some other problem please provide some more specifics and I'll fix it.

Thanks,

/jens
GeneralRe: Demo project corrupted. Pin
Anonymous6-Feb-03 22:06
Anonymous6-Feb-03 22:06 
GeneralRe: Demo project corrupted. Pin
Jens Nilsson7-Feb-03 8:20
Jens Nilsson7-Feb-03 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.