Jump to content

Recommended Posts

Posted

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()
As
Byte
= mem.ToArray

' The compressed bytes in memory (Retval) and and those written to file are same length

Return
retval

Catch
ex
As
Exception

Return
Nothing

Finally

If
Not
g
Is
Nothing
Then

g.Close()

End
If

If
Not
mem
Is
Nothing
Then

mem.Close()

End
If

End
Try

EndUsing

EndFunction

 

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

 

PublicFunction 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)
As
Byte

fs.Position = 0

Dim
fileread
As
Integer
= g2.Read(filebuffer, 0, filebuffer.Length)

While
fileread > 0

mem3.Write(filebuffer, 0, fileread)

fileread = g2.Read(filebuffer, 0, filebuffer.Length)

End
While

Dim
decompfile()
As
Byte
= 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)
As
Byte

Dim
memread
As
Integer
= 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)

End
While

' therefore retval is empty

Dim
retval()
As
Byte
= mem2.ToArray

Return
retval

Catch
ex
As
Exception

Return
Nothing

Finally

If
Not
mem
Is
Nothing
Then

mem.Close()

End
If

If
Not
mem2
Is
Nothing
Then

mem2.Close()

End
If

If
Not
g
Is
Nothing
Then

g.Flush()

g.Close()

End
If

End
Try

EndUsing

ReturnNothing

EndFunction

 

 

More...

 

View All Our Microsoft Related Feeds

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...