Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompressing .bz2 stream to file in C#

I'm having real trouble trying to use the .bz2 stuff from the SharpZipLib library and I've not been able to find any help else where.

Any help or advice would be much appreciated and if anyone can point me at an existing solution I can learn from that would be fantastic!

Below is what I'm trying to do, but obviously it's not working. Currently the problem I'm having is an 'EndOfStreamException' being unhandled on the marked line. The code is a bit of a mess, I've never tried to do anything like this before...

As you can probably tell I'm downloading this from the web before decompressing it, I'm fairly sure that part of the code works correctly though.

while ((inputByte = responseStream.ReadByte()) != -1)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (BinaryWriter writer = new BinaryWriter(ms))
        {
            writer.Write((byte)inputByte);
        }

        using (BZip2InputStream unzip = new BZip2InputStream(ms)) //Exception occurs here
        {
            buffer = new byte[unzip.Length];
            unzip.Read(buffer, 0, buffer.Length);
        }

        using (FileStream fs = new FileStream(fPath, FileMode.OpenOrCreate, FileAccess.Write))
        {
            fs.Write(buffer, 0, buffer.Length);
        }

    }
}
like image 971
Mandatory Avatar asked Dec 22 '12 00:12

Mandatory


1 Answers

You are using using statements. using statements are compiler directives for a try finally to be wrapped around the block of code and IDisposible.Dispose() will be called when the finally is executed.

Long story short, calling dispose on the BinaryWriter, BZip2InputStream, and FileStream are probably prematurely disposing of the parent MemoryStream.

Try removing the three using blocks from within the MemoryStream and see if that fixes your issue.

Edit

Your BinaryWriter is writing a single byte to the MemoryStream. I don't believe you need a BinaryWriter for this as MemoryStream has a WriteByte() method.

Then your BZip2InputStream is trying to read from the MemoryStream. But the MemoryStream has it's position at the end of the stream. There is no data to read, hence the EndOfStreamException.

like image 77
Jason Whitted Avatar answered Sep 22 '22 12:09

Jason Whitted