Decompress, Extract, Unzip file with GZipStream Class in C#

Code snippet for decompressing file with GZipStream class in C# (Apply for .net 4.0). GZipStream class is for compressing and decompressing file(s) into gzip (read about gzip format on wikipedia) format which is based on based on the DEFLATE algorithm. So data is read in on a byte-by-byte basis, so you can not perform multiple passes to determine the best method for compressing entire files or large blocks of data.

following code is for decompressing or extract a file in c# :

private void Decompress(FileInfo fi, string unZipedFilesPath)
   {
     try
       {
         // Get the stream of the source file.
          using (FileStream inFile = fi.OpenRead())
           {
             // Get orignial file extension, for example "doc" from report.doc.gz.
             string curFile = fi.Name;
             dynamic origName = curFile.Remove(curFile.Length - fi.Extension.Length);
             // Create the decompressed file.
             Directory.CreateDirectory(unZipedFilesPath + "\\" + origName);
             using (FileStream outFile = File.Create(unZipedFilesPath + "\\" + origName + "\\" + 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);
                   }
                }
             }
         }
       catch (Exception ex)
       {
       }
   }

How to use above function:

DirectoryInfo di = new DirectoryInfo(dirpath);
// Decompress all *.gz files in the directory.
foreach (FileInfo fi in di.GetFiles("*.gz"))
{
   Decompress(fi);
}

There is a limitation when you are using gZipStream class, you might not be able to decompress data that results in over 8 GB of uncompressed data.