Write Method

DeflateStream.Write Method (Byte[], Int32, Int32)

 

Writes compressed bytes to the underlying stream from the specified byte array.

Namespace:   System.IO.Compression
Assembly:  System (in System.dll)

public override void Write(
	byte[] array,
	int offset,
	int count
)

Parameters

array
Type: System.Byte[]

The buffer that contains the data to compress.

offset
Type: System.Int32

The byte offset in array from which the bytes will be read.

count
Type: System.Int32

The maximum number of bytes to write.

The following example shows how to compress and decompress bytes by using the Read and Write methods.

using System;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace ExampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            UnicodeEncoding uniEncode = new UnicodeEncoding();

            byte[] bytesToCompress = uniEncode.GetBytes("example text to compress and decompress");
            Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress));

            using (FileStream fileToCompress = File.Create("examplefile.gz"))
            {
                using (DeflateStream compressionStream = new DeflateStream(fileToCompress, CompressionMode.Compress))
                {
                    compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length);
                }
            }

            byte[] decompressedBytes = new byte[bytesToCompress.Length];
            using (FileStream fileToDecompress = File.Open("examplefile.gz", FileMode.Open))
            {
                using (DeflateStream decompressionStream = new DeflateStream(fileToDecompress, CompressionMode.Decompress))
                {
                    decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
                }
            }

            Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes));
        }
    }
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft