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

BZip2 Classes

, 10 Feb 2003
Rate this:
Please Sign up or sign in to vote.
Two classes to compress/decompress data using bzip2
<!-- Download Links --> <!-- Article image -->

Sample Image - BZ2Class.jpg

<!-- Add the rest of your HTML here -->

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

About the Author

Gilad Novik
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

 
Generalsomething not so good PinmemberLiaoheping6-Jul-06 22:54 
i notice that in the
 
virtual unsigned int OnCompressRead(char *&pBuffer)
{ // Read some data from our source file
return m_fileSource.Read(pBuffer=m_szReadBuffer,
sizeof(m_szReadBuffer));
}
 
how do you know the exactly lenght of input data for compress?
 
if my data is less or longer then the readbuf's lenght,i don't
think so it will work fine
 
your alway's friend from china
GeneralRe: something not so good PinmemberLiuYongPeng28-Oct-06 7:14 
Generali need a little help Pinmemberserumas16-May-05 10:26 
GeneralRe: i need a little help PinmemberGilad Novik16-May-05 13:59 
GeneralRe: i need a little help Pinmemberserumas17-May-05 9:17 
Generalperformance and efficiency Pinmemberseaskyzhang11-Aug-03 3:48 
GeneralRe: performance and efficiency PinmemberGilad Novik11-Aug-03 4:04 
GeneralRe: performance and efficiency Pinmemberseaskyzhang11-Aug-03 4:25 
GeneralRe: performance and efficiency PinmemberGilad Novik11-Aug-03 5:20 
GeneralBugs PinmemberMiroslav Rajcic5-Mar-03 21:37 
GeneralRe: Bugs PinmemberMiroslav Rajcic6-Mar-03 20:28 
GeneralRe: Bugs PinmemberGilad Novik8-Mar-03 7:44 
GeneralRe: Bugs Pinmemberfifi11-Jul-03 18:09 
Generalplease help me error PinmemberBoscoW21-Feb-03 8:54 
GeneralCompression ratio as opposed to zlib... PinmemberAlberto Gattegno19-Feb-03 20:23 
GeneralRe: Compression ratio as opposed to zlib... PinmemberGilad Novik19-Feb-03 20:31 
GeneralGood one PinmemberShay Harel18-Feb-03 16:22 
Generalur profile... Pinmemberlauren16-Feb-03 9:39 
GeneralA BIG BUG!!! PinmemberFelix HAWKEYE16-Feb-03 9:34 
Generalvery good PinmemberAhmed Ismaiel Zakaria12-Feb-03 5:33 

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.140718.1 | Last Updated 11 Feb 2003
Article Copyright 2003 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid