Click here to Skip to main content
15,867,834 members
Articles / Desktop Programming / MFC
Article

Compress Data

Rate me:
Please Sign up or sign in to vote.
2.85/5 (24 votes)
17 Apr 2003 87.7K   3.3K   25   14
Compress/uncompress data in CFile, CByteArray and IStream(ISequentialStream)

Introduction

This code will compress/decompress data from ISequentialStream to ISequentialStream. It also has functions to compress/decompress CFile and CByteArray.

Background

While developing software, sometimes I need to compress data to decrease size. The gzip library does compression for me, but it is not ready to handle in-memory or CFile in MFC. So, here is the class that assists me compressing data.

Using the code

The class Flate does compression/decompression for ISquentialStream, CFile, CByteArray. Compression for CByteArray is done by another small utility class CBAStreamReader/CBAStreamWriter.

//
// class Flate declaration.
//
class Flate
{
public:
    Flate()
    {
    }

    void Inflate(ISequentialStream* pOutput, ISequentialStream* pInput);
    void Deflate(ISequentialStream* pOutput, ISequentialStream* pInput);

    void Inflate(CFile* pOutput, CFile* pInput);
    void Deflate(CFile* pOutput, CFile* pInput);

    BOOL    Compress(CByteArray& dst, const CByteArray& src)
    {
        return Compress(dst, src.GetData(), src.GetSize());
    }

    BOOL    Compress(CByteArray& dst, const BYTE* src, UINT srcLen);

    BOOL    Uncompress(CByteArray& dst, const CByteArray& src)
    {
        return Uncompress(dst, src.GetData(), src.GetSize());
    }

    BOOL    Uncompress(CByteArray& dst, const BYTE* src, UINT srcLen);
};

Here is one function that compresses CFile data. The internal function uses z_stream to support custom streams. First, it reads data from input CFile, compresses it by deflate method and saves it to output CFile.

//
// Deflate method compresses CFile based data.
//

void Flate::Deflate(CFile* pOutput, CFile* pInput)
{
    z_stream zstm;
    memset(&zstm,0,sizeof(z_stream));

    BYTE zBufIn[UNCOMP_BUFSIZE];
    BYTE zBufOut[COMP_BUFSIZE];

    deflateInit(&zstm, Z_DEFAULT_COMPRESSION);

    int err = Z_OK;
    while ( TRUE )
    {
        UINT cbRead = 0;
        cbRead = pInput->Read(zBufIn, sizeof(zBufIn));
        if ( cbRead == 0 )
            break;

        zstm.next_in = (Bytef*)zBufIn;
        zstm.avail_in = (uInt)cbRead;

        while ( TRUE )
        {
            zstm.next_out = (Bytef*)zBufOut;
            zstm.avail_out = sizeof(zBufOut);

            err = deflate(&zstm, Z_SYNC_FLUSH);
            if (err != Z_OK)
                break;

            UINT cbWrite = sizeof(zBufOut) - zstm.avail_out;
            if ( cbWrite == 0 )
                break;
            pOutput->Write(zBufOut, cbWrite);

            if ( zstm.avail_out != 0 )
                break;
        }
    }

    err = deflateEnd(&zstm);
}

Here is the other function that uncompresses CFile data. It is almost same as Inflate.

//
// Inflate methods uncompresses CFile based data.
//

void Flate::Inflate(CFile* pOutput, CFile* pInput)
{
    z_stream zstm;
    memset(&zstm,0,sizeof(z_stream));

    BYTE zBufIn[COMP_BUFSIZE];
    BYTE zBufOut[UNCOMP_BUFSIZE];

    inflateInit(&zstm);

    int err = Z_OK;
    while ( TRUE )
    {
        UINT cbRead = 0;
        cbRead = pInput->Read(zBufIn, sizeof(zBufIn));
        if ( cbRead == 0 )
            break;

        zstm.next_in = (Bytef*)zBufIn;
        zstm.avail_in = (uInt)cbRead;

        while ( TRUE )
        {
            zstm.next_out = (Bytef*)zBufOut;
            zstm.avail_out = sizeof(zBufOut);

            err = inflate(&zstm, Z_SYNC_FLUSH);
            if (err != Z_OK)
                break;

            UINT cbWrite = sizeof(zBufOut) - zstm.avail_out;
            if ( cbWrite == 0 )
                break;
            pOutput->Write(zBufOut, cbWrite);

            if ( zstm.avail_out != 0 )
                break;
        }
    }

    err = inflateEnd(&zstm);
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
navid_gharehkhanian2-Jan-10 9:49
navid_gharehkhanian2-Jan-10 9:49 
GeneralLinking error : on deflateInit(....) call Pin
mmayur11-Feb-09 16:56
mmayur11-Feb-09 16:56 
Generalhelp me Pin
ven12319-May-05 20:15
ven12319-May-05 20:15 
Generalhelp...help...help Pin
wiguna25-Mar-05 18:59
wiguna25-Mar-05 18:59 
QuestionHow to compress Image Pin
pubududilena23-Jul-04 19:40
pubududilena23-Jul-04 19:40 
Questioncan not compress more than 4096 bytes Pin
michaelv9-Jun-04 5:15
michaelv9-Jun-04 5:15 
AnswerRe: can not compress more than 4096 bytes Pin
MiracleMike1-Sep-04 5:23
MiracleMike1-Sep-04 5:23 
GeneralClueless !! Pin
WREY21-Apr-03 22:16
WREY21-Apr-03 22:16 
GeneralUpdate article Pin
James Lee18-Apr-03 15:35
James Lee18-Apr-03 15:35 
GeneralPlease give us a clue. Pin
Anonymous18-Apr-03 10:07
Anonymous18-Apr-03 10:07 
GeneralRe: Please give us a clue. Pin
Anonymous18-Apr-03 10:17
Anonymous18-Apr-03 10:17 
GeneralRe: Please give us a clue. Pin
dog_spawn18-Apr-03 13:35
dog_spawn18-Apr-03 13:35 
GeneralRe: Please give us a clue. Pin
Marc Clifton18-Apr-03 15:05
mvaMarc Clifton18-Apr-03 15:05 
GeneralPlease update article Pin
peterchen18-Apr-03 0:55
peterchen18-Apr-03 0:55 

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.