Click here to Skip to main content
11,639,861 members (60,654 online)
Click here to Skip to main content

Compress Data

, 17 Apr 2003 71.9K 2.9K 25
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

James Lee
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
navid_gharehkhanian2-Jan-10 9:49
membernavid_gharehkhanian2-Jan-10 9:49 
GeneralLinking error : on deflateInit(....) call Pin
mayur8u11-Feb-09 16:56
membermayur8u11-Feb-09 16:56 
Generalhelp me Pin
ven12319-May-05 20:15
memberven12319-May-05 20:15 
Generalhelp...help...help Pin
wiguna25-Mar-05 18:59
memberwiguna25-Mar-05 18:59 
QuestionHow to compress Image Pin
pubba23-Jul-04 19:40
susspubba23-Jul-04 19:40 
Questioncan not compress more than 4096 bytes Pin
michaelv9-Jun-04 5:15
membermichaelv9-Jun-04 5:15 
AnswerRe: can not compress more than 4096 bytes Pin
MiracleMike1-Sep-04 5:23
memberMiracleMike1-Sep-04 5:23 
GeneralClueless !! Pin
WREY21-Apr-03 22:16
memberWREY21-Apr-03 22:16 
GeneralUpdate article Pin
James Lee18-Apr-03 15:35
memberJames Lee18-Apr-03 15:35 
GeneralPlease give us a clue. Pin
Anonymous18-Apr-03 10:07
sussAnonymous18-Apr-03 10:07 
GeneralRe: Please give us a clue. Pin
Anonymous18-Apr-03 10:17
sussAnonymous18-Apr-03 10:17 
GeneralRe: Please give us a clue. Pin
dog_spawn18-Apr-03 13:35
memberdog_spawn18-Apr-03 13:35 
GeneralRe: Please give us a clue. Pin
Marc Clifton18-Apr-03 15:05
memberMarc Clifton18-Apr-03 15:05 
GeneralPlease update article Pin
peterchen18-Apr-03 0:55
memberpeterchen18-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 | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 18 Apr 2003
Article Copyright 2003 by James Lee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid