Imports System.IO
Imports Xceed.Compression
Imports Xceed.Compression.Formats
Dim sourceStream As New FileStream("d:\data.txt", FileMode.Open)
' If you do not want the inner stream to be closed by the GZipCompressedStream
' then set the GZipCompressedStream's Transient property to true.
Dim destinationStream As New MemoryStream()
Dim gzip As New GZipCompressedStream(destinationStream)
'32K at a time.
Dim buffer(32768) As Byte
Dim bytesRead As Integer = 0
' Loop until we have nothing more to read from the source stream.
Do
bytesRead = sourceStream.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Then
gzip.Write(buffer, 0, bytesRead)
End If
Loop Until bytesRead = 0
' Close the source stream and the GZipCompressedStream.
'
' Because the GZipCompressedStream will automatically close the destination
' memory stream, there is no need to call Close once we are done with the stream.
sourceStream.Close()
gzip.Close()
' To get access to the MemoryStream's compressed data, you can use
' Dim compressedData() As Byte = destinationStream.ToArray()
' ToArray() works even when the memory stream is closed.