Click here to Skip to main content
Click here to Skip to main content

Compress Data

By , 17 Apr 2003
Rate this:
Please Sign up or sign in to vote.

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

About the Author

James Lee

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembernavid_gharehkhanian2-Jan-10 9:49 
GeneralLinking error : on deflateInit(....) call Pinmembermayur8u11-Feb-09 16:56 
Generalhelp me Pinmemberven12319-May-05 20:15 
Generalhelp...help...help Pinmemberwiguna25-Mar-05 18:59 
QuestionHow to compress Image Pinsusspubba23-Jul-04 19:40 
Questioncan not compress more than 4096 bytes Pinmembermichaelv9-Jun-04 5:15 
AnswerRe: can not compress more than 4096 bytes PinmemberMiracleMike1-Sep-04 5:23 
GeneralClueless !! PinmemberWREY21-Apr-03 22:16 
GeneralUpdate article PinmemberJames Lee18-Apr-03 15:35 
GeneralPlease give us a clue. PinsussAnonymous18-Apr-03 10:07 
GeneralRe: Please give us a clue. PinsussAnonymous18-Apr-03 10:17 
GeneralRe: Please give us a clue. Pinmemberdog_spawn18-Apr-03 13:35 
GeneralRe: Please give us a clue. PinmemberMarc Clifton18-Apr-03 15:05 
GeneralPlease update article Pinmemberpeterchen18-Apr-03 0:55 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 Apr 2003
Article Copyright 2003 by James Lee
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid