Click here to Skip to main content
15,883,940 members
Articles / Programming Languages / C++
Article

BZip2 Classes

Rate me:
Please Sign up or sign in to vote.
4.66/5 (24 votes)
10 Feb 20031 min read 113.9K   2K   43   20
Two classes to compress/decompress data using bzip2

Sample Image - BZ2Class.jpg

Introduction

In my previous project, I had to include an option for making backup of the data. Since I didn't want to use some external tools (such as pkzip), I've decided to include a compression library by myself.

I've checked zlib and libbzip2. Eventually, I had to use bzip2 since I need small backup file and this library give better compression results

To make life simple, I made 2 classes to handle usage of compression/decompression

Using the code

class CBZ2Base
{
protected:
    CBZ2Base(char* pWriteBuffer,unsigned int nWriteBufferSize);
protected:
    bz_stream m_Stream;
    char* m_pWriteBuffer;
    unsigned int m_nWriteBufferSize;
};
The base class is CBZ2Base. The main classes (CBZ2Compress,CBZ2Decompress) are derived from the base class. It contains a private variable (m_Stream) to handle bzip2 library.
class CBZ2Compress : CBZ2Base
{
public:
    CBZ2Compress(char* pWriteBuffer,unsigned int nWriteBufferSize,<BR>        int nBlockSize=9,int nWorkFactor=30);
    virtual ~CBZ2Compress();

public:
    unsigned int Compress();

protected:
    virtual unsigned int OnCompressRead(char* &pBuffer)=0;
    virtual void OnCompressWrite(const char* pBuffer,unsigned int nLength)=0;
};

class CBZ2Decompress : CBZ2Base
{
public:
    CBZ2Decompress(char* pWriteBuffer,unsigned int nWriteBufferSize,<BR>        int nSmall=false);
    virtual ~CBZ2Decompress();

public:
    unsigned int Decompress();

protected:
    virtual unsigned int OnDecompressRead(char* &pBuffer)=0;
    virtual void OnDecompressWrite(const char* pBuffer,<BR>        unsigned int nLength)=0;
};
You need to allocate a buffer for the output data and pass it to the constructor. Each class contains two virtual functions for reading and writing of data. You need to override them and use your own code to fill the buffer when reading and handling the buffer when writing.

A simple code to handle file compressing:

class CMyCompression : public CBZ2Compress
{
public:
    CMyCompression() : CBZ2Compress(m_szWriteBuffer,sizeof(m_szWriteBuffer))
    {    // Initialize with our buffer
    }
    BOOL Start(LPCTSTR szSource,LPCTSTR szTarget)
    {
        BOOL bResult=FALSE;    // Assume failure
        if (m_fileSource.Open(szSource,CFile::modeRead))
        {
            if (m_fileTarget.Open(szTarget,<BR>                CFile::modeCreate|CFile::modeWrite))
            {
                bResult=(Compress()>0);    <BR>                  // Function should return packed buffer length<BR>
                m_fileTarget.Close();
            }
            m_fileSource.Close();
        }
        return bResult;
    }
        
protected:
    virtual unsigned int OnCompressRead(char *&pBuffer)
    {    // Read some data from our source file
        return m_fileSource.Read(pBuffer=m_szReadBuffer,<BR>             sizeof(m_szReadBuffer));
    }
    void OnCompressWrite(const char *pBuffer, unsigned int nLength)
    {    // Write some data to our target file
        m_fileTarget.Write(pBuffer,nLength);
    }
private:
    CFile m_fileSource,m_fileTarget;
    char m_szReadBuffer[4096],m_szWriteBuffer[4096];
};

void main()
{
    CMyCompression a;
    a.Start(_T("test.tmp"),_T("test.bz2"));
}
Similar code will be used for decompressing

The included file contains a static library with both classes. There is also a simple demo to compress/decompress a file. You can check the demo source code to learn some more about using the classes.

Copyright

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) You are allowed to modify the source code in any way you want. I would appreciate any acknowledgment from you regarding the usage of the classes

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
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

Comments and Discussions

 
GeneralBugs Pin
Miroslav Rajcic5-Mar-03 21:37
Miroslav Rajcic5-Mar-03 21:37 
GeneralRe: Bugs Pin
Miroslav Rajcic6-Mar-03 20:28
Miroslav Rajcic6-Mar-03 20:28 
GeneralRe: Bugs Pin
Gilad Novik8-Mar-03 7:44
Gilad Novik8-Mar-03 7:44 
GeneralRe: Bugs Pin
fifi11-Jul-03 18:09
fifi11-Jul-03 18:09 

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.