compress and decompress byte arrays

  • Thread starter Thread starter Carly
  • Start date Start date
C

Carly

Hello,

I am just wondering if somebody can look on this piece of code and let
me know what is wrong with DECOMPRESSION.

Dim mystr As String = "flower power"
'deflate
Dim mybytearray() As Byte
Dim mybytearraytarget() As Byte
mybytearray = Encoding.Default.GetBytes(mystr)
Dim mystream As New MemoryStream
Dim mystreami As New MemoryStream
Dim objdeflate As New DeflateStream(mystream,
CompressionMode.Compress)
objdeflate.Write(mybytearray, 0, mybytearray.Length)
mybytearraytarget = mystream.ToArray
mystream.Close()
'decompress
Dim objinflate As New DeflateStream(mystreami,
CompressionMode.Decompress)
objinflate.Read(mybytearraytarget, 0, mybytearraytarget.Length)

'at this poing mystreami is empty. I am wondering why?

Dim sr As New StreamReader(mystreami)
MsgBox(sr.ReadToEnd)

I wonder also if anybody can share a compress/decompress code segment
(in VB) different from the one MS has on their online help.


Thank you,

Carly
 
'decompress
Dim objinflate As New DeflateStream(mystreami,
CompressionMode.Decompress)
objinflate.Read(mybytearraytarget, 0, mybytearraytarget.Length)

'at this poing mystreami is empty. I am wondering why?

Because you've never written anything to the stream. The DeflateStream
class will read compressed data from the mystreami stream (if there
were any), decompress it and write to mybytearraytarget.


Mattias
 
Back
Top