Problem with GzZipStream

Joined
Jan 10, 2007
Messages
43,898
Location
In The Machine
Hi there, can anyone point me in the direction*of where I'm going wrong here.* I have written two functions, basically the same as countless examples on web..
- One is passed an array of bytes as a parameter and returns a compressed array of bytes
- The other (surprise surprise) is passed the compressed array and (is meant to) return an uncompressed array.

I have temporarily added code to parallel the compression/decompression to file, curious to see if that worked.

The gist of this is, the "compression function" writes the same number of bytes to file as it returns as an array.
The "decompression" function decompresses the file version back to the original size, but the attempt to do simlar on memory is failing.

The point that it seems to go awry is documented.**After positioning the underlying MemoryStream position to 0, a read of N bytes returns zero read, and the MemoryStream Position is now at end*of stream.*

Any help will*be greatly appreciated.

---------------------------------------------------------------------------------------

PublicFunction Compress(ByRef inbytes() AsByte) AsByte()
Using mem AsNew MemoryStream, _
g
AsNew GZipStream(mem, CompressionMode.Compress), _
fs
AsNew FileStream("c:\users\davem\documents\compressedtable.gz", FileMode.OpenOrCreate, FileAccess.Write), _
g2
AsNew GZipStream(fs, CompressionMode.Compress)
Try
'========================================================================
' First compress array of bytes to file
g2.Write(inbytes, 0, inbytes.Length)
'=========================================================================
' Now compress to an array (retVal)
g.Write(inbytes, 0, inbytes.Length)
Dim retval() AsByte = mem.ToArray
' The compressed bytes in memory (Retval) and and those written to file are same length
Return retval
Catch ex As Exception
ReturnNothing
Finally
IfNot g IsNothingThen
g.Close()
EndIf
IfNot mem IsNothingThen
mem.Close()
EndIf
EndTry
EndUsing
EndFunction

----------------------------------------------------------

Public
Function DeCompress(ByRef inbytes() AsByte) AsByte()
Using mem As MemoryStream = New MemoryStream(inbytes), _
mem2
As MemoryStream = New MemoryStream(), _
g As GZipStream = New GZipStream(mem, CompressionMode.Decompress, False), _
fs
As FileStream = New FileStream("c:\users\davem\documents\compressedtable.gz", FileMode.Open, FileAccess.Read), _
g2 As GZipStream = New GZipStream(fs, CompressionMode.Decompress, True)
Try
'=============================================================================
' First decompress the file
Dim filebuffer(1024) AsByte
fs.Position = 0
Dim fileread AsInteger = g2.Read(filebuffer, 0, filebuffer.Length)
While fileread > 0
mem3.Write(filebuffer, 0, fileread)
fileread = g2.Read(filebuffer, 0, filebuffer.Length)
EndWhile
Dim decompfile() AsByte = mem3.ToArray
' decompfile array is the same size as original uncompressed byte array
'==============================================================================
' Now try decompressing the array of compressed bytes
mem.Position = 0
Dim buffer(1024) AsByte
Dim memread AsInteger = g.Read(buffer, 0, buffer.Length)
' memread is zero, and the mem.Position is now positioned at end of stream
' so this next loop is of course skipped entirely
While memread > 0
mem2.Write(buffer, 0, memread)
memread = g.Read(buffer, 0, buffer.Length)
EndWhile
' therefore retval is empty
Dim retval() AsByte = mem2.ToArray
Return retval
Catch ex As Exception
ReturnNothing
Finally
IfNot mem IsNothingThen
mem.Close()
EndIf
IfNot mem2 IsNothingThen
mem2.Close()
EndIf
IfNot g IsNothingThen
g.Flush()
g.Close()
EndIf
EndTry
EndUsing
ReturnNothing
EndFunction



More...

View All Our Microsoft Related Feeds
 
Back
Top