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);
        }

    }
}
有帮助吗?

解决方案

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.

其他提示

There are two problems in the OP's code. First one, as Jason Whitted mentioned, is that the using code block of the BinaryWriter causes disposal of the MemoryStream instance, so we should remove the block and because MemoryStream itself has a write method, this can be achieved in the most clean way using the following:

var ms = new MemoryStream();
ms.Write(inputBytes,0,inputBytes.Length); 

The second problem is that when we write to the MemoryStream instance (ms), we go to the end of it, so before we try using BZip2InputStream we must change the position back to the beginning of the memory stream:

ms.Seek(0, SeekOrigin.Begin); //THIS IS NECESSARY to change the position to the beginning
byte[] buffer = null;
using (var bz2InputStream = new BZip2InputStream(ms))
{
       buffer = new byte[bz2InputStream.Length];
       bz2InputStream.Read(buffer, 0, buffer.Length);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top