.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

function to decompress the string in C#

Posted By:Rajshree       Posted Date: September 20, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

function to decompress the string in C#

        /// This method deCompressed the string passed in the
        /// parameter

        /// String to decompress
        public static string DecompressString(this String content_)
            if (string.IsNullOrEmpty(content_))
                return string.Empty;

            GZipStream zip = null;
            MemoryStream memoryStream = null;

                byte[] gzBuffer = Convert.FromBase64String(content_);
                memoryStream = new MemoryStream();

                int contentLength = BitConverter.ToInt32(gzBuffer, 0);
                memoryStream.Write(gzBuffer, 4, gzBuffer.Length - 4);

                byte[] buffer = new byte[contentLength];

                memoryStream.Position = 0;
                zip = new GZipStream(memoryStream, CompressionMode.Decompress);

                zip.Read(buffer, 0, buffer.Length);

                return Encoding.UTF8.GetString(buffer);
            /* Catch the format exception, Some of the old data may be in
             * decompressed mode So in this case only return the content
             * passed as paramert.
            catch (FormatException)
                return content_;
                #region Release Objects

                if (zip != null)
                    zip = null;

                if (memoryStream != null)
                    memoryStream = null;



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend