GZipStream.Read Method

Reads a number of decompressed bytes into the specified byte array.

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

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

Parameters

array
Type: System.Byte[]

The array used to store decompressed bytes.

offset
Type: System.Int32

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

count
Type: System.Int32

The maximum number of decompressed bytes to read.

Return Value

Type: System.Int32
The number of bytes that were decompressed into the byte array. If the end of the stream has been reached, zero or the number of bytes read is returned.

ExceptionCondition
ArgumentNullException

array is null.

InvalidOperationException

The CompressionMode value was Compress when the object was created.

- or -

The underlying stream does not support reading.

ArgumentOutOfRangeException

offset or count is less than zero.

-or-

array length minus the index starting point is less than count.

InvalidDataException

The data is in an invalid format.

ObjectDisposedException

The stream is closed.

If data is found in an invalid format, an InvalidDataException is thrown as one of the last operations. A cyclic redundancy check (CRC) is performed as one of the last operations of this method.

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 (GZipStream compressionStream = new GZipStream(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 (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
                {
                    decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
                }
            }

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft