compress and decompress byte arrays

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
 
M

Mattias Sjögren

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top