Click here to Skip to main content
13,559,184 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 10 Feb 2003

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


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
    CBZ2Base(char* pWriteBuffer,unsigned int nWriteBufferSize);
    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
    CBZ2Compress(char* pWriteBuffer,unsigned int nWriteBufferSize,<BR>        int nBlockSize=9,int nWorkFactor=30);
    virtual ~CBZ2Compress();

    unsigned int Compress();

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

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

    unsigned int Decompress();

    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
    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>
        return bResult;
    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
    CFile m_fileSource,m_fileTarget;
    char m_szReadBuffer[4096],m_szWriteBuffer[4096];

void main()
    CMyCompression a;
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.


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


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"

You may also be interested in...

Comments and Discussions

Generalsomething not so good Pin
Liaoheping6-Jul-06 22:54
memberLiaoheping6-Jul-06 22:54 
GeneralRe: something not so good Pin
LiuYongPeng28-Oct-06 7:14
memberLiuYongPeng28-Oct-06 7:14 
Generali need a little help Pin
serumas16-May-05 10:26
memberserumas16-May-05 10:26 
GeneralRe: i need a little help Pin
Gilad Novik16-May-05 13:59
memberGilad Novik16-May-05 13:59 
GeneralRe: i need a little help Pin
serumas17-May-05 9:17
memberserumas17-May-05 9:17 
Generalperformance and efficiency Pin
seaskyzhang11-Aug-03 3:48
memberseaskyzhang11-Aug-03 3:48 
GeneralRe: performance and efficiency Pin
Gilad Novik11-Aug-03 4:04
memberGilad Novik11-Aug-03 4:04 
GeneralRe: performance and efficiency Pin
seaskyzhang11-Aug-03 4:25
memberseaskyzhang11-Aug-03 4:25 
GeneralRe: performance and efficiency Pin
Gilad Novik11-Aug-03 5:20
memberGilad Novik11-Aug-03 5:20 
GeneralBugs Pin
Miroslav Rajcic5-Mar-03 21:37
memberMiroslav Rajcic5-Mar-03 21:37 
GeneralRe: Bugs Pin
Miroslav Rajcic6-Mar-03 20:28
memberMiroslav Rajcic6-Mar-03 20:28 
GeneralRe: Bugs Pin
Gilad Novik8-Mar-03 7:44
memberGilad Novik8-Mar-03 7:44 
GeneralRe: Bugs Pin
fifi11-Jul-03 18:09
memberfifi11-Jul-03 18:09 
Generalplease help me error Pin
BoscoW21-Feb-03 8:54
memberBoscoW21-Feb-03 8:54 
GeneralCompression ratio as opposed to zlib... Pin
<marquee>Alberto Gattegno19-Feb-03 20:23
memberAlberto Gattegno19-Feb-03 20:23 
GeneralRe: Compression ratio as opposed to zlib... Pin
Gilad Novik19-Feb-03 20:31
memberGilad Novik19-Feb-03 20:31 
GeneralGood one Pin
Shay Harel18-Feb-03 16:22
memberShay Harel18-Feb-03 16:22 
Generalur profile... Pin
lauren16-Feb-03 9:39
memberlauren16-Feb-03 9:39 
GeneralA BIG BUG!!! Pin
Felix HAWKEYE16-Feb-03 9:34
memberFelix HAWKEYE16-Feb-03 9:34 
Generalvery good Pin
Ahmed Ismaiel Zakaria12-Feb-03 5:33
memberAhmed 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 11 Feb 2003
Article Copyright 2003 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid