GZipStream Class
Updated: January 2012
Provides methods and properties used to compress and decompress streams.
Assembly: System (in System.dll)
The GZipStream type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | GZipStream(Stream, CompressionMode) | Initializes a new instance of the GZipStream class using the specified stream and CompressionMode value. |
![]() ![]() | GZipStream(Stream, CompressionMode, Boolean) | Initializes a new instance of the GZipStream class using the specified stream and CompressionMode value, and a value that specifies whether to leave the stream open. |
| Name | Description | |
|---|---|---|
![]() ![]() | BaseStream | Gets a reference to the underlying stream. |
![]() ![]() | CanRead | Gets a value indicating whether the stream supports reading while decompressing a file. (Overrides Stream.CanRead.) |
![]() ![]() | CanSeek | Gets a value indicating whether the stream supports seeking. (Overrides Stream.CanSeek.) |
![]() ![]() | CanTimeout | Gets a value that determines whether the current stream can time out. (Inherited from Stream.) |
![]() ![]() | CanWrite | Gets a value indicating whether the stream supports writing. (Overrides Stream.CanWrite.) |
![]() ![]() | Length | This property is not supported and always throws a NotSupportedException. (Overrides Stream.Length.) |
![]() ![]() | Position | This property is not supported and always throws a NotSupportedException. (Overrides Stream.Position.) |
![]() ![]() | ReadTimeout | Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out. (Inherited from Stream.) |
![]() ![]() | WriteTimeout | Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out. (Inherited from Stream.) |
| Name | Description | |
|---|---|---|
![]() ![]() | BeginRead | Begins an asynchronous read operation. (Overrides Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).) |
![]() ![]() | BeginWrite | Begins an asynchronous write operation. (Overrides Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object).) |
![]() ![]() | Close | Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. (Inherited from Stream.) |
![]() | CopyTo(Stream) | Reads the bytes from the current stream and writes them to the destination stream. (Inherited from Stream.) |
![]() | CopyTo(Stream, Int32) | Reads all the bytes from the current stream and writes them to a destination stream, using a specified buffer size. (Inherited from Stream.) |
![]() | CreateObjRef | Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) |
![]() ![]() | CreateWaitHandle | Obsolete. Allocates a WaitHandle object. (Inherited from Stream.) |
![]() ![]() | Dispose() | Releases all resources used by the Stream. (Inherited from Stream.) |
![]() ![]() | Dispose(Boolean) | Releases the unmanaged resources used by the GZipStream and optionally releases the managed resources. (Overrides Stream.Dispose(Boolean).) |
![]() ![]() | EndRead | Waits for the pending asynchronous read to complete. (Overrides Stream.EndRead(IAsyncResult).) |
![]() ![]() | EndWrite | Handles the end of an asynchronous write operation. (Overrides Stream.EndWrite(IAsyncResult).) |
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | Flush | Flushes the contents of the internal buffer of the current GZipStream object to the underlying stream. (Overrides Stream.Flush().) |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | InitializeLifetimeService | Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() ![]() | MemberwiseClone() | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.) |
![]() | ObjectInvariant | Infrastructure. Provides support for a Contract. (Inherited from Stream.) |
![]() ![]() | Read | Reads a number of decompressed bytes into the specified byte array. (Overrides Stream.Read(Byte[], Int32, Int32).) |
![]() ![]() | ReadByte | Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. (Inherited from Stream.) |
![]() ![]() | Seek | This property is not supported and always throws a NotSupportedException. (Overrides Stream.Seek(Int64, SeekOrigin).) |
![]() ![]() | SetLength | This property is not supported and always throws a NotSupportedException. (Overrides Stream.SetLength(Int64).) |
![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() ![]() | Write | Writes compressed bytes to the underlying stream from the specified byte array. (Overrides Stream.Write(Byte[], Int32, Int32).) |
![]() ![]() | WriteByte | Writes a byte to the current position in the stream and advances the position within the stream by one byte. (Inherited from Stream.) |
This class represents the gzip data format, which uses an industry standard algorithm for lossless file compression and decompression. The format includes a cyclic redundancy check value for detecting data corruption. The gzip data format uses the same algorithm as the DeflateStream class, but can be extended to use other compression formats. The format can be readily implemented in a manner not covered by patents.
Compressed GZipStream objects written to a file with an extension of .gz can be decompressed using many common compression tools; however, this class does not inherently provide functionality for adding files to or extracting files from .zip archives.
The GZipStream class might not be able to decompress data that results in over 8 GB of uncompressed data.
The compression functionality in DeflateStream and GZipStream is exposed as a stream. Data is read in on a byte-by-byte basis, so it is not possible to perform multiple passes to determine the best method for compressing entire files or large blocks of data. The DeflateStream and GZipStream classes are best used on uncompressed sources of data. If the source data is already compressed, using these classes may actually increase the size of the stream.
Notes to InheritorsWhen you inherit from GZipStream, you must override the following members: CanSeek, CanWrite, and CanRead.
The following example shows how to use the GZipStream class to compress and decompress a directory of files.
using System; using System.IO; using System.IO.Compression; namespace zip { public class Program { public static void Main() { // Path to directory of files to compress and decompress. string dirpath = @"c:\users\public\reports"; DirectoryInfo di = new DirectoryInfo(dirpath); // Compress the directory's files. foreach (FileInfo fi in di.GetFiles()) { Compress(fi); } // Decompress all *.gz files in the directory. foreach (FileInfo fi in di.GetFiles("*.gz")) { Decompress(fi); } } public static void Compress(FileInfo fi) { // Get the stream of the source file. using (FileStream inFile = fi.OpenRead()) { // Prevent compressing hidden and // already compressed files. if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fi.Extension != ".gz") { // Create the compressed file. using (FileStream outFile = File.Create(fi.FullName + ".gz")) { using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) { // Copy the source file into // the compression stream. inFile.CopyTo(Compress); Console.WriteLine("Compressed {0} from {1} to {2} bytes.", fi.Name, fi.Length.ToString(), outFile.Length.ToString()); } } } } } public static void Decompress(FileInfo fi) { // Get the stream of the source file. using (FileStream inFile = fi.OpenRead()) { // Get original file extension, for example // "doc" from report.doc.gz. string curFile = fi.FullName; string origName = curFile.Remove(curFile.Length - fi.Extension.Length); //Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { // Copy the decompression stream // into the output file. Decompress.CopyTo(outFile); Console.WriteLine("Decompressed: {0}", fi.Name); } } } } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
