Implementation of GZipStream compression
GZipStream class provides methods which can be used to
compress and decompress data or files into a zip archive.
Listing 1 – Function to compress Byte array data
and returns compressed Byte array
''' <summary>
''' Byte to Byte compression.
''' </summary>
''' <param name="byteSource"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function CompressByte(ByVal byteSource() As Byte) As Byte()
' Create a GZipStream object and memory stream object to store compressed stream
Dim objMemStream As New MemoryStream()
Dim objGZipStream As New GZipStream(objMemStream, CompressionMode.Compress, True)
objGZipStream.Write(byteSource, 0, byteSource.Length)
objGZipStream.Dispose()
objMemStream.Position = 0
' Write compressed memory stream into byte array
Dim buffer(objMemStream.Length) As Byte
objMemStream.Read(buffer, 0, buffer.Length)
objMemStream.Dispose()
Return buffer
End Function
Description
·
objGZipStream.Write (byteSource, 0,
byteSource.Length) converts the source byte array into compressed stream
and writes it into GZipStream object.
·
objMemStream.Position = 0 sets the
current position of the compressed stream to the beginning. Note that it is
necessary to position at 0 for point it the header of stream.
·
objMemStream.Read(buffer, 0, buffer.Length)
writes the compressed memory stream into byte array buffer.
Listing 2 – Function to compress Byte array data
and returns compressed memory stream
''' <summary>
''' Byte to stream compression.
''' </summary>
''' <param name="byteSource"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function CompressData(ByVal byteSource As Byte()) As MemoryStream
' Create the streams and byte arrays needed
Dim byteBuffer As Byte() = Nothing
Dim objSourceMemStream As MemoryStream = Nothing
Dim objDestinationStream As New MemoryStream
Dim compressedStream As GZipStream = Nothing
objSourceMemStream = New MemoryStream(byteSource)
byteBuffer = New Byte(objSourceMemStream.Length) {}
Dim checkCounter As Integer = objSourceMemStream.Read(byteBuffer, 0, byteBuffer.Length)
compressedStream = New GZipStream(objDestinationStream, CompressionMode.Compress, True)
' close all streams
If Not (objSourceMemStream Is Nothing) Then
objSourceMemStream.Close()
End If
If Not (compressedStream Is Nothing) Then
compressedStream.Close()
End If
If Not (objDestinationStream Is Nothing) Then
objDestinationStream.Close()
End If
' Return compressed array of bytes
Return objDestinationStream
End Function
Description
·
objSourceMemStream = New
MemoryStream(byteSource) initializes memory stream with the source byte
array.
·
objSourceMemStream.Read(byteBuffer, 0,
byteBuffer.Length) reads the source stream values into the output byte
array(byteBuffer() in this example).
·
compressedStream = New
GZipStream(objDestinationStream, CompressionMode.Compress, True) create
a compressed stream pointing to the destination stream.
Listing 3 – Function to decompress the compressed
Byte array
''' <summary>
''' Decompress the byte array (compressed) by using GZipStream class library.
''' </summary>
''' <param name="byteCompressed">Compressed Byte()</param>
''' <returns>Decompressed Byte()</returns>
''' <remarks></remarks>
Public Function DecompressByte(ByVal byteCompressed() As Byte) As Byte()
Try
' Initialize memory stream with byte array.
Dim objMemStream As New MemoryStream(byteCompressed)
' Initialize GZipStream object with memory stream.
Dim objGZipStream As New GZipStream(objMemStream, CompressionMode.Decompress)
' Define a byte array to store header part from compressed stream.
Dim sizeBytes(3) As Byte
' Read the size of compressed stream.
objMemStream.Position = objMemStream.Length - 5
objMemStream.Read(sizeBytes, 0, 4)
Dim iOutputSize As Integer = BitConverter.ToInt32(sizeBytes, 0)
' Posistion the to point at beginning of the memory stream to read
' compressed stream for decompression.
objMemStream.Position = 0
Dim decompressedBytes(iOutputSize - 1) As Byte
' Read the decompress bytes and write it into result byte array.
objGZipStream.Read(decompressedBytes, 0, iOutputSize)
objGZipStream.Dispose()
objMemStream.Dispose()
Return decompressedBytes
Catch ex As Exception
Return Nothing
End Try
End Function
Description
·
objMemStream.Position =
objMemStream.Length - 5 and objMemStream.Read(sizeBytes,
0, 4) reads the size of the actual stream which is coded at the last portion
in compressed stream.
·
objGZipStream.Read(decompressedBytes,
0, iOutputSize) reads the decompress bytes and write it into result byte
array.
Implementation of GZipStream decompression
Listing 4 – Function to decompress the compressed
memory stream
''' <summary>
''' Function to decompress the memory stream(compressed) by using GZipStream class library.
''' Note that, it is important for the caller to do the compressing with GZipStream.
''' </summary>
''' <param name="compressedStream">MemoryStream (compressed)</param>
''' <param name="buffLength">Required size of result byte()</param>
''' <returns>Byte() (decompressed)</returns>
''' <remarks></remarks>
Public Function DecompressData(ByVal compressedStream As MemoryStream, _
ByVal buffLength As ULong) As Byte()
compressedStream.Position = 0
Dim zipStream As New GZipStream(compressedStream, CompressionMode.Decompress)
Dim decompressedBuffer(buffLength + 100) As Byte
' Use the ReadAllBytesFromStream to read the stream.
Dim totalCount As Integer = ReadAllBytesFromStream(zipStream, decompressedBuffer)
Return decompressedBuffer
End Function
Description
·
compressedStream.Position = 0 resets
the memory stream position to begin decompression.
·
Dim zipStream As New
GZipStream(compressedStream, CompressionMode.Decompress) converts
compressed stream into uncompressed stream.
·
ReadAllBytesFromStream(zipStream,
decompressedBuffer) is used to get the size of the uncompressed byte
array from uncompressed stream. See Listing 5 for the definition of function
ReadAllBytesFromStream.
Listing 5 – Function to get the required maximum
size of byte array for memory stream
''' <summary>
''' To get size of byte of the memory stream.
''' </summary>
''' <param name="stream">Input memory stream</param>
''' <param name="buffer"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ReadAllBytesFromStream(ByVal stream As Stream, _
ByVal buffer As Byte()) As Integer
' Use this method is used to read all bytes from a stream.
Dim offset As Integer = 0
Dim totalCount As Integer = 0
While True
Dim bytesRead As Integer = stream.Read(buffer, offset, 100)
If bytesRead = 0 Then
Exit While
End If
offset + = bytesRead
totalCount + = bytesRead
End While
Return totalCount
End Function 'ReadAllBytesFromStream
Description
Function ReadAllBytesFromStream
reads memory stream in loop of 100 bytes each and returns the total size. You
can specify other values to the maximum number of bytes to read from stream.