Export (0) Print
Expand All

GZipStream.EndRead Method

Waits for the pending asynchronous read to complete. (Consider using the the Stream.ReadAsync method instead; see the Remarks section.)

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

public override int EndRead(
	IAsyncResult asyncResult
)

Parameters

asyncResult
Type: System.IAsyncResult

The reference to the pending asynchronous request to finish.

Return Value

Type: System.Int32
The number of bytes read from the stream, between 0 (zero) and the number of bytes you requested. GZipStream returns 0 only at the end of the stream; otherwise, it blocks until at least one byte is available.

ExceptionCondition
ArgumentNullException

asyncResult is null.

ArgumentException

asyncResult did not originate from a BeginRead method on the current stream.

InvalidOperationException

The end operation cannot be performed because the stream is closed.

Starting with the .NET Framework 4.5, you can perform asynchronous read operations by using the Stream.ReadAsync method. The EndRead method is still available in .NET Framework 4.5 to support legacy code; however, you can implement asynchronous I/O operations more easily by using the new async methods. For more information, see Asynchronous File I/O.

Call this method to determine how many bytes were read from the stream. This method can be called once to return the amount of bytes read between calls to BeginRead and EndRead.

This method blocks until the I/O operation has completed.

The following code example shows how to use the GZipStream class to compress and decompress a file.

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

namespace zip
{
    public class Program
    {
        private static string directoryPath = @"c:\temp";
        public static void Main()
        {
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
            Compress(directorySelected);

            foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
            {
                Decompress(fileToDecompress);
            }
        }

        public static void Compress(DirectoryInfo directorySelected)
        {
            foreach (FileInfo fileToCompress in directorySelected.GetFiles())
            {
                using (FileStream originalFileStream = fileToCompress.OpenRead())
                {
                    if ((File.GetAttributes(fileToCompress.FullName) & 
                       FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                    {
                        using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                        {
                            using (GZipStream compressionStream = new GZipStream(compressedFileStream, 
                               CompressionMode.Compress))
                            {
                                originalFileStream.CopyTo(compressionStream);

                            }
                        }
                        FileInfo info = new FileInfo(directoryPath + "\\" + fileToCompress.Name + ".gz");
                        Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                        fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());
                    }

                }
            }
        }

        public static void Decompress(FileInfo fileToDecompress)
        {
            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            {
                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                using (FileStream decompressedFileStream = File.Create(newFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                        Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                    }
                }
            }
        }
    }
}

.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
Show:
© 2015 Microsoft