Xceed .NET Libraries Documentation
Compressing

This topic demonstrates how to compress data that is read from a FileStream to a MemoryStream using the GZipCompressedStream class.  

Although we are using a FileStream as our source and a MemoryStream as our destination, we could have also used any other type of stream. For example, we could have compressed data read from a MemoryStream to another MemoryStream or to a FileStream.

Basic steps

To compress data that is read from a stream, the following steps must be taken:

  1. Create the source stream from which the data will be read. For the purposes of this example, our source stream will be a FileStream. 

  2. Create the destination stream to which the compressed data will be written. For the purposes of this example, our destination stream will be a MemoryStream. 

  3. Create a GZipCompressedStream around the destination stream. For the purposes of this example we used the GZipCompressedStream class however we could have also used the XceedCompressedStream or the ZLibCompressedStream classes. 

  4. Read the data from the source FileStream. 

  5. Write the compressed data to the destination MemoryStream. Creating the GZipCompressedStream around the destination stream means that the data that is written to the destination MemoryStream will be compressed as it is written.

Demonstration

The following example demonstrates how to read data from a FileStream and compress it to a MemoryStream using the GZipCompressedStream class.

VB.NET Copy Code

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.

C# Copy Code
using System.IO;
using Xceed.Compression;
using Xceed.Compression.Formats;
 
using( FileStream sourceStream = new FileStream( @"d :\data.txt", FileMode.Open ) )
{
  // Because the GZipCompressedStream will automatically close the destination
  // memory stream, there is no need to declare the memory stream within a using
  // statement or to call Close once we are done with the stream.
  //
  // If you do not want the inner stream to be closed by the GZipCompressedStream
  // then set the GZipCompressedStream's Transient property to true.
  MemoryStream destinationStream = new MemoryStream();
       
  using( GZipCompressedStream gzip = new GZipCompressedStream( destinationStream ) )
  {
    // 32K at a time.
    byte[] buffer = new byte[ 32768 ];
    int bytesRead = 0;
 
    // Loop until we have nothing more to read from the source stream.
    while( ( bytesRead = sourceStream.Read( buffer, 0, buffer.Length  ) ) > 0 )
    {
      gzip.Write( buffer, 0, bytesRead );
    }
  }
     
  // To get access to the MemoryStream's compressed data, you can use
  // byte[] compressedData = destinationStream.ToArray();
  // ToArray() works even when the memory stream is closed.
}

Things you should consider

The main questions you should ask yourself when compressing data read from a stream are: