Xceed Real-Time Zip for .NET Documentation
Write Method (CompressedStream)
Example 


Xceed.Compression Assembly > Xceed.Compression Namespace > CompressedStream Class : Write Method
An array of bytes. A maximum of count bytes are taken from this array and compressed into the inner stream.
The byte offset in buffer at which to begin taking the data.
The maximum number of bytes to be taken from buffer.
Compresses and writes a sequence of bytes to the inner stream.
Syntax
'Declaration
 
Public Overrides Sub Write( _

   ByVal buffer() As Byte, _

   ByVal offset As Integer, _

   ByVal count As Integer _

) 
'Usage
 
Dim instance As CompressedStream

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. A maximum of count bytes are taken from this array and compressed into the inner stream.
offset
The byte offset in buffer at which to begin taking the data.
count
The maximum number of bytes to be taken from buffer.

Return Value

The total number of bytes written to the inner stream. This may be less than the count parameter. It may even be 0.
Remarks

In order to achieve optimum compression, some compression algorithms store data to compress into internal buffers before actually compressing it.

When Write returns, all the data provided in the buffer parameter has been successfully given to the compressor, but has not necessarily been written to the inner stream.

The return value of this method represents the number of compressed bytes that have been actually written to the inner stream.

If you need to write all pending data to the inner stream, you can call the Flush method on the CompressedStream.

If the writing process to compress the byte array has already begun, CanRead will always return false.

Example
The following example demonstrates how to read data from a FileStream and compress it to a MemoryStream using the CompressedStream class.
Imports System.IO

Imports Xceed.Compression



Dim sourceStream As New FileStream("d:\data.txt", FileMode.Open)



' If you do not want the inner stream to be closed by the CompressedStream

' then set the CompressedStream's Transient property to true.



Dim destinationStream As New MemoryStream() Dim compStream As New CompressedStream( destinationStream )



'32K at at 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

        compStream.Write(buffer, 0, bytesRead)

    End If

Loop Until bytesRead = 0



' Close the source stream and the CompressedStream.

'

' Because the CompressedStream will automatically close the destination

' memory stream, there is no need to call Close once we are done with the stream.

sourceStream.Close()

compStream.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( FileStream sourceStream = new FileStream( @"d:\data.txt", FileMode.Open ) )

{

    // Because the CompressedStream 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 CompressedStream

    // then set the CompressedStream's Transient property to true.

    MemoryStream destinationStream = new MemoryStream();

    

    using( CompressedStream compStream = new CompressedStream( destinationStream ) )

    {

        // 32K at at 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 )

        {

            compStream.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

Reference

CompressedStream Class
CompressedStream Members
Base Implementation in Write