CopyTo in GZipStream Class

In VS2010 we found a new function CopyTo in GZipStream Class.

By the help of this function we can unzip a large size of zip file without use of any ByteBuffer array.

In the previous version of Visual Studio we used either a third party dll of byte buffer array to Unzip any file but buffer has some own limitation so if the file size is very large i.e in GB then it threw and overflow exception.

But now in visual studio 2010 by the use of GZipStream Class and CopyTo method we can Unzip any size of file.

 in the following function in fi parameter you need to pass your own file information.

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)

            {

            }

        }

 

Ebook Download
View all
Learn
View all