Click here to Skip to main content
14,365,424 members

.NET wrapper for libbz2

Rate this:
4.44 (11 votes)
Please Sign up or sign in to vote.
4.44 (11 votes)
14 Jul 2003
.NET wrapper for libbz2, written in MC++


BzipStream is a small .NET wrapper for the Bzip2 compression utility. Bz2 compression is similar to Gzip where only one file/chunk/blob is compressed, unlike zip, rar, ace, etc. Its normally used to compress a tarball (.tar file).

Below is the implementation diagram:

public class BzipStream : Stream
  public BzipStream(string filename, FileAccess mode);
  public override void Close();
  public static byte[] Compress(byte[] buffer);
  public static byte[] Compress(byte[] buffer, int level);
  public static byte[] Decompress(byte[] buffer);
  public override void Flush();
  public static BzipStream OpenRead(string filename);
  public static BzipStream OpenWrite(string filename);
  public override int Read(byte[] buffer, int start, int length);
  public override long Seek(long pos, SeekOrigin origin);
  public override void SetLength(long len);
  public override void Write(byte[] buffer, int start, int length);
  public override bool CanRead { get; }
  public override bool CanSeek { get; }
  public override bool CanWrite { get; }
  public override long Length { get; }
  public override long Position { get; set; }

The De/Compress methods are utility methods to quickly de/compress a byte[]. The following methods are implemented but not supported:

  • Length
  • Position
  • Seek

Also FileAccess.ReadWrite is NOT supported.

Usage (C#)

BzipStream bz = new BzipStream("file.bz2", FileAccess.Write);

Stream s = File.OpenRead("file");
int len = 8192;
byte[] buffer = new byte[len];
while ((len = s.Read(buffer, 0, len)) > 0)
  bz.Write(buffer, 0, len);

//or using utility methods
byte[] cbuff = BzipStream.Compress(buffer);

buffer = BzipStream.Decompress(cbuff);


libbz2 is compiled to vanilla C, and a single file (dotbz2.cpp) carries the MC++ implementation (and ONLY that file has "managed" compile flags set). Many people have been asking how to do this. Very easy. Just look.

The basic pattern is:

  1. Get .NET object
  2. Allocate memory on the heap
  3. Copy object and get an IntPtr
  4. Cast the IntPtr to void*
  5. Cast to <T>*
  6. Do unmanaged function
  7. Cast result to void*
  8. Cast to IntPtr
  9. Copy from heap to object
  10. Free heap memory

Steps 7 and 8 are not really necessary as we have a reference to the IntPtr already.


Just a small example of how to wrap a C library in MC++. Suggestions welcome. I could paste the code, its only 185 lines. Don't flame me for the length of the article. :)


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

Software Developer
South Africa South Africa
No Biography provided

Comments and Discussions

GeneralStream Compress and buffer compress Pin
staceyw14-Oct-04 11:53
memberstaceyw14-Oct-04 11:53 
GeneralWhy system can't find the file Pin
williamlwl29-Mar-04 2:47
memberwilliamlwl29-Mar-04 2:47 
QuestionCAB format compression? Pin
dragomir17-Feb-04 23:18
memberdragomir17-Feb-04 23:18 
QuestionSo... what do I do with it? Pin
fadden22-Oct-03 15:37
memberfadden22-Oct-03 15:37 
AnswerRe: So... what do I do with it? Pin
fadden23-Oct-03 14:33
memberfadden23-Oct-03 14:33 
GeneralRe: So... what do I do with it? Pin
Member 779040727-Mar-11 21:04
memberMember 779040727-Mar-11 21:04 
GeneralAnother inspiring article by leppie Pin
Stephane Rodriguez.16-Jul-03 11:00
memberStephane Rodriguez.16-Jul-03 11:00 
GeneralRe: Another inspiring article by leppie Pin
leppie16-Jul-03 14:12
memberleppie16-Jul-03 14:12 
Generalagain Pin
ROK_RShadow15-Jul-03 14:51
memberROK_RShadow15-Jul-03 14:51 
GeneralRe: again Pin
Jan van den Baard16-Jul-03 7:31
professionalJan van den Baard16-Jul-03 7:31 
GeneralRe: again Pin
leppie16-Jul-03 8:09
memberleppie16-Jul-03 8:09 
GeneralRe: again Pin
ROK_RShadow16-Jul-03 19:11
memberROK_RShadow16-Jul-03 19:11 
GeneralRe: again Pin
leppie17-Jul-03 8:01
memberleppie17-Jul-03 8:01 
GeneralRe: again Pin
Paul Watson16-Jul-03 8:19
sitebuilderPaul Watson16-Jul-03 8:19 
GeneralBTW Pin
Jörgen Sigvardsson15-Jul-03 10:35
memberJörgen Sigvardsson15-Jul-03 10:35 
GeneralRe: BTW Pin
leppie15-Jul-03 10:45
memberleppie15-Jul-03 10:45 
GeneralRe: BTW Pin
Jörgen Sigvardsson15-Jul-03 11:18
memberJörgen Sigvardsson15-Jul-03 11:18 
GeneralVery off topic Pin
Jörgen Sigvardsson15-Jul-03 10:35
memberJörgen Sigvardsson15-Jul-03 10:35 
GeneralRe: Very off topic Pin
leppie15-Jul-03 10:43
memberleppie15-Jul-03 10:43 

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.

Posted 14 Jul 2003


20 bookmarked