G
Guest
HI,
I tried compression in VS 2005.
Sample code bellow. Very simple task to compress byte array and then
decompress it. Compression works fine but decompression doesn't. When I use
FileStream instead of MemoryStream it works both ways. I write file and then
read it. It's probably some stupid mistake I make in decompress function. Can
anybody help?
Dim bt() As Byte =
Compress(System.Text.UnicodeEncoding.Unicode.GetBytes("AAAAAAAAAAAA")
Dim bt2() As Byte = Decompress(bt)
Public Function Compress(ByVal body() As Byte) As Byte()
Try
Dim ms As New MemoryStream()
Dim st As New GZipStream(ms, CompressionMode.Compress, True)
Dim bt(body.Length - 1) As Byte
'compress
st.Write(bt, 0, bt.Length)
st.Flush()
st.Close()
st.Dispose()
'read compressed
ms.Position = 0
Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)
ms.Close()
ms.Dispose()
Return buffer
Catch ex As Exception
Debug.WriteLine("Compress: " + ex.ToString())
Throw ex
End Try
End Function
Public Function Decompress(ByVal body() As Byte) As Byte()
Try
Dim ms As New MemoryStream(body)
Dim gz As New GZipStream(ms, CompressionMode.Decompress)
Dim bt(3) As Byte
ms.Position = ms.Length - 4
ms.Read(bt, 0, 4)
ms.Position = 0
Dim size As Integer = BitConverter.ToInt32(bt, 0)
Dim buffer(size + 100) As Byte
Dim offset As Integer = 0
Dim total As Integer = 0
While (True)
Dim j As Integer = gz.Read(buffer, offset, 100) 'DOESN'T WORK
If j = 0 Then Exit While
offset += j
total += j
End While
gz.Close()
gz.Dispose()
ms.Close()
ms.Dispose()
Dim ra(total - 1) As Byte
Array.ConstrainedCopy(buffer, 0, ra, 0, total)
Return ra
Catch ex As Exception
Debug.WriteLine("Decompress: " + ex.ToString())
Throw ex
End Try
End Function
Thanks
Mark
I tried compression in VS 2005.
Sample code bellow. Very simple task to compress byte array and then
decompress it. Compression works fine but decompression doesn't. When I use
FileStream instead of MemoryStream it works both ways. I write file and then
read it. It's probably some stupid mistake I make in decompress function. Can
anybody help?
Dim bt() As Byte =
Compress(System.Text.UnicodeEncoding.Unicode.GetBytes("AAAAAAAAAAAA")
Dim bt2() As Byte = Decompress(bt)
Public Function Compress(ByVal body() As Byte) As Byte()
Try
Dim ms As New MemoryStream()
Dim st As New GZipStream(ms, CompressionMode.Compress, True)
Dim bt(body.Length - 1) As Byte
'compress
st.Write(bt, 0, bt.Length)
st.Flush()
st.Close()
st.Dispose()
'read compressed
ms.Position = 0
Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)
ms.Close()
ms.Dispose()
Return buffer
Catch ex As Exception
Debug.WriteLine("Compress: " + ex.ToString())
Throw ex
End Try
End Function
Public Function Decompress(ByVal body() As Byte) As Byte()
Try
Dim ms As New MemoryStream(body)
Dim gz As New GZipStream(ms, CompressionMode.Decompress)
Dim bt(3) As Byte
ms.Position = ms.Length - 4
ms.Read(bt, 0, 4)
ms.Position = 0
Dim size As Integer = BitConverter.ToInt32(bt, 0)
Dim buffer(size + 100) As Byte
Dim offset As Integer = 0
Dim total As Integer = 0
While (True)
Dim j As Integer = gz.Read(buffer, offset, 100) 'DOESN'T WORK
If j = 0 Then Exit While
offset += j
total += j
End While
gz.Close()
gz.Dispose()
ms.Close()
ms.Dispose()
Dim ra(total - 1) As Byte
Array.ConstrainedCopy(buffer, 0, ra, 0, total)
Return ra
Catch ex As Exception
Debug.WriteLine("Decompress: " + ex.ToString())
Throw ex
End Try
End Function
Thanks
Mark