IO.Compression.DeflateStream Error
Ok, so here is my problem. I am trying to use DeflateStream to decompress a packet. I found an example of how to do this on the msdn site, however every time I run it I get the error "Block length does not match with its complement."
I have searched a ton on this error and found a whole lot of people with the problem and none with a solution. Below is the code I am using to Deflate the packet. Any help would be appreciated.
Code:
Public Shared Function ReadAllBytesFromStream(ByVal stream As Stream, ByVal buffer() As Byte) As Byte()
' Use this method is used to read all bytes from a stream.
Dim offset As Integer = 0
Dim totalCount As Integer = 0
While True
'This is the line that errors
Dim bytesRead As Integer = stream.Read(buffer, offset, 100)
If bytesRead = 0 Then
Exit While
End If
offset += bytesRead
totalCount += bytesRead
End While
Return buffer
End Function
Public Shared Function DeflateCompressDecompress(ByVal Data As Byte()) As Byte()
Try
' Open the file as a FileStream object.
Dim ms As New MemoryStream(Data)
ms.Position = 0
Dim zipStream As New DeflateStream(ms, CompressionMode.Decompress)
Dim decompressedBuffer(Data.Length + 100) As Byte
' Use the ReadAllBytesFromStream to read the stream.
Dim returnData As Byte() = DeflateTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
zipStream.Close()
Return returnData
Catch e As Exception
Return Nothing
End Try
End Function
Re: IO.Compression.DeflateStream Error
Their compression example leaves a lot to be desired. I'm in the process of switching over to the J# ZIP implementation that somebody posted in the codebank here. A lot less code and it works a lot faster and better even if it means distributing an extra install package.
The biggest problem I had with their code example was that it played fast and loose with type conversion (Option Strict was not used) and in closing and properly disposing of streams. It also didn't get the filenames correct if there were multiple periods in the name and used questionable string concatenation methods. I went back through a lot of the code fixed this and, as I recall, this took care of a lot of the errors I had with it. I'm still wasn't that that happy with it so that's why I've moved away from it.
If you're early on in the project, go to the J# ZIP solution if you can. Otherwise, I'd say turn on Option Strict, go through the code and fix that, then go through the code and make sure all streams are disposed of properly and that strings are being handled right. If you do this it should work, I got it to, but, as I said, I'm not that comfortable with the result.