Friday, October 24, 2008

Compression in .NET

I am currently working on several projects which require managing large sets of data. Commonly, compression is used as part of the solution. The .NET framework offers compression libraries that compress files up to 99% of their decompressed size. Below is a function written in VB that reports a comparison of the decompressed version of a file with the compressed version of the same file in a message box.

Details of the compression algorithm used can be found at http://www.gzip.org/algorithm.txt .

Reports a comparison of the decompressed version of a file with the compressed version of the same file in a message box.

Private Sub CompressAndDecompressFile(ByVal filename As String)
Dim infile As FileStream
Try
' Open the file as a FileStream object.
infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim buffer(infile.Length - 1) As Byte
' Read the file to ensure it is readable.
Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
If count <> buffer.Length Then
infile.Close()
msg = "Test Failed: Unable to read data from file"
MsgBox(msg)
Return
End If
infile.Close()
Dim ms As New MemoryStream()
' Use the newly created memory stream for the compressed data.
Dim compressedzipStream As New GZipStream(ms, CompressionMode.Compress, True)
compressedzipStream.Write(buffer, 0, buffer.Length)
' Close the stream.
compressedzipStream.Close()

msg = "Original size: " & buffer.Length & ", Compressed size: " & ms.Length
MsgBox(msg)

' Reset the memory stream position to begin decompression.
ms.Position = 0
Dim zipStream As New GZipStream(ms, CompressionMode.Decompress)
Dim decompressedBuffer(buffer.Length + buffer_size) As Byte
' Use the ReadAllBytesFromStream to read the stream.
Dim totalCount As Integer = ReadAllBytesFromStream(zipStream, decompressedBuffer)
msg = "Decompressed " & totalCount & " bytes"
MsgBox(msg)

zipStream.Close()
Catch ex As Exception
msg = "Error: The file being read contains invalid data."
MsgBox(msg)
End Try
End Sub

Public Shared 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, buffer_size)
If bytesRead = 0 Then
Exit While
End If
offset += bytesRead
totalCount += bytesRead
End While
Return totalCount
End Function 'ReadAllBytesFromStream

No comments: