Xceed .NET Libraries Documentation
Xceed.Compression.Formats Assembly / Xceed.Compression.Formats Namespace / GZipCompressedStream Class / Write Method
An array of bytes containing the compressed data. This method copies count bytes from buffer to the current stream.
The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
The number of bytes to be written to the current stream.
Example


In This Topic
    Write Method (GZipCompressedStream)
    In This Topic
    Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
    Syntax
    'Declaration
     
    Public Overrides Sub Write( _
       ByVal buffer() As Byte, _
       ByVal offset As Integer, _
       ByVal count As Integer _
    ) 
    'Usage
     
    Dim instance As GZipCompressedStream
    Dim buffer() As Byte
    Dim offset As Integer
    Dim count As Integer
     
    instance.Write(buffer, offset, count)
    public override void Write( 
       byte[] buffer,
       int offset,
       int count
    )

    Parameters

    buffer
    An array of bytes containing the compressed data. This method copies count bytes from buffer to the current stream.
    offset
    The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
    count
    The number of bytes to be written to the current stream.
    Remarks

    Use the CanWrite property to determine whether the current instance supports writing.

    If the write operation is successful, the position within the stream advances by the number of bytes written. If an exception occurs, the position within the stream remains unchanged.

    Example
    The following example demonstrates how to read data from a FileStream and compress it to a MemoryStream using the GZipCompressedStream class.
    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.
    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.
    }
    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also