Click here to Skip to main content
Licence 
First Posted 14 Jul 2003
Views 104,751
Bookmarked 19 times

.NET wrapper for libbz2

By | 14 Jul 2003 | Article
.NET wrapper for libbz2, written in MC++

BzipStream

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);
}
s.Close();
bz.Close();

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

buffer = BzipStream.Decompress(cbuff);

Implementation

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.

Conclusion

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. :)

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

leppie

Software Developer

South Africa South Africa

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralStream Compress and buffer compress Pinmemberstaceyw10:53 14 Oct '04  
GeneralWhy system can't find the file Pinmemberwilliamlwl1:47 29 Mar '04  
QuestionCAB format compression? Pinmemberdragomir22:18 17 Feb '04  
QuestionSo... what do I do with it? Pinmemberfadden14:37 22 Oct '03  
AnswerRe: So... what do I do with it? Pinmemberfadden13:33 23 Oct '03  
GeneralRe: So... what do I do with it? PinmemberMember 779040720:04 27 Mar '11  
GeneralAnother inspiring article by leppie PinmemberStephane Rodriguez.10:00 16 Jul '03  
GeneralRe: Another inspiring article by leppie Pinmemberleppie13:12 16 Jul '03  
Generalagain PinmemberROK_RShadow13:51 15 Jul '03  
GeneralRe: again PinmemberJan van den Baard6:31 16 Jul '03  
GeneralRe: again Pinmemberleppie7:09 16 Jul '03  
GeneralRe: again PinmemberROK_RShadow18:11 16 Jul '03  
GeneralRe: again Pinmemberleppie7:01 17 Jul '03  
GeneralRe: again PinsitebuilderPaul Watson7:19 16 Jul '03  
GeneralBTW PinmemberJörgen Sigvardsson9:35 15 Jul '03  
GeneralRe: BTW Pinmemberleppie9:45 15 Jul '03  
GeneralRe: BTW PinmemberJörgen Sigvardsson10:18 15 Jul '03  
GeneralVery off topic PinmemberJörgen Sigvardsson9:35 15 Jul '03  
GeneralRe: Very off topic Pinmemberleppie9:43 15 Jul '03  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 15 Jul 2003
Article Copyright 2003 by leppie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid