I am writing some code to compress a byte array into a smaller byte array. Then I would like to decompress it:
''' <summary>
''' Receives bytes, returns compressed bytes.
''' </summary>
Function Compress(ByRef raw() As Byte) As Byte()
Using memory As MemoryStream = New MemoryStream()
Using gzip As GZipStream = New GZipStream(memory, CompressionMode.Compress)
gzip.Write(raw, 0, raw.Length)
End Using
Return memory.ToArray()
End Using
End Function
''' <summary>
''' Receives compressed bytes, returns bytes.
''' </summary>
Function DeCompress(ByRef compress() As Byte) As Byte()
Using memory As MemoryStream = New MemoryStream()
Using gzip As GZipStream = New GZipStream(memory, CompressionMode.Decompress)
gzip.Write(compress, 0, compress.Length)
End Using
Return memory.ToArray()
End Using
End Function
(I adopted this code from here)
My compression code works but my decompression code gives the following error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: Writing to the compression stream is not supported.
I've tried many variations of gzip.Read
and swapping around variables. (If I knew how to peer into the VB.NET inner source code, like I can with the JDK perhaps I could reverse engineer my way to a solutions, oh well)
How can I retool my DeCompress
function to work as intended?
EDIT:
I noticed I got down voted becuase I didn't show the .Read
method use. Well I can't follow through with the .Read
algo because my VB.NET does not have a .CopyTo()
function. I don't understand why: