Click here to Skip to main content
15,867,901 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.7K   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

 
Generalsomething not so good Pin
Liaoheping6-Jul-06 22:54
Liaoheping6-Jul-06 22:54 
GeneralRe: something not so good Pin
LiuYongPeng28-Oct-06 7:14
LiuYongPeng28-Oct-06 7:14 
Generali need a little help Pin
serumas16-May-05 10:26
serumas16-May-05 10:26 
GeneralRe: i need a little help Pin
Gilad Novik16-May-05 13:59
Gilad Novik16-May-05 13:59 
GeneralRe: i need a little help Pin
serumas17-May-05 9:17
serumas17-May-05 9:17 
Generalperformance and efficiency Pin
Seasky.Zhang11-Aug-03 3:48
Seasky.Zhang11-Aug-03 3:48 
GeneralRe: performance and efficiency Pin
Gilad Novik11-Aug-03 4:04
Gilad Novik11-Aug-03 4:04 
GeneralRe: performance and efficiency Pin
Seasky.Zhang11-Aug-03 4:25
Seasky.Zhang11-Aug-03 4:25 
GeneralRe: performance and efficiency Pin
Gilad Novik11-Aug-03 5:20
Gilad Novik11-Aug-03 5:20 
GeneralBugs Pin
Miroslav Rajcic5-Mar-03 21:37
Miroslav Rajcic5-Mar-03 21:37 
Hi!

I like your class, but it seems that there are bugs involved (not sure is it your code or the bzip2 library).

I downloaded few .bz2 files from internet
(one of them is http://www.php.net/distributions/manual/php_manual_en.tar.bz2
=> see links at page http://www.php.net/download-docs.php).
This file was not decompressed correctly. To eliminate suspicion of bad archive I have tried few more .bz2 files and lot of them failed to decompress.

I have noticed if I change the buffer size in your class from 16384 or something to lets say 4k, I get bad results with files I compress and then try to decompress.

Can you verify this ?

Best regards,

Miroslav Rajcic
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 
Generalplease help me error Pin
BoscoW21-Feb-03 8:54
BoscoW21-Feb-03 8:54 
GeneralCompression ratio as opposed to zlib... Pin
Alberto Bar-Noy19-Feb-03 20:23
Alberto Bar-Noy19-Feb-03 20:23 
GeneralRe: Compression ratio as opposed to zlib... Pin
Gilad Novik19-Feb-03 20:31
Gilad Novik19-Feb-03 20:31 
GeneralGood one Pin
Shay Harel18-Feb-03 16:22
Shay Harel18-Feb-03 16:22 
Generalur profile... Pin
l a u r e n16-Feb-03 9:39
l a u r e n16-Feb-03 9:39 
GeneralA BIG BUG!!! Pin
Felix HAWKEYE16-Feb-03 9:34
Felix HAWKEYE16-Feb-03 9:34 
Generalvery good Pin
Ahmed Ismaiel Zakaria12-Feb-03 5:33
Ahmed Ismaiel Zakaria12-Feb-03 5:33 

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.