Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 403583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:13:48+00:00 2026-05-12T17:13:48+00:00

What is wrong with this code below. I always get FALSE, meaning after compression,

  • 0

What is wrong with this code below. I always get FALSE, meaning after compression, decompressed data does not match original value.

public static bool Test()
        {
            string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] data = encoding.GetBytes(sample);
            bool result = false;

            //Compress
            MemoryStream cmpStream;
            cmpStream = new MemoryStream();
            GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress);
            hgs.Write(data, 0, data.Length);
            byte[] cmpData = cmpStream.ToArray();

            MemoryStream decomStream;
            decomStream = new MemoryStream(cmpData);
            hgs = new GZipStream(decomStream, CompressionMode.Decompress);
            hgs.Read(data, 0, data.Length);

            string sampleOut = System.BitConverter.ToString(data);

            result = String.Equals(sample, sampleOut) ;
            return result;
        }

I will really appreciate if you can point out where I am making a mistake.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:13:49+00:00Added an answer on May 12, 2026 at 5:13 pm

    Try this code:

    public static bool Test()
            {
                string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
    
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    
                byte[] data = encoding.GetBytes(sample);
                bool result = false;
    
                // Compress
                MemoryStream cmpStream = new MemoryStream();
    
                GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress);
    
                hgs.Write(data, 0, data.Length);
    
                byte[] cmpData = cmpStream.ToArray();
    
                MemoryStream decomStream = new MemoryStream(cmpData);
    
                hgs = new GZipStream(decomStream, CompressionMode.Decompress);
                hgs.Read(data, 0, data.Length);
    
                string sampleOut = encoding.GetString(data);
    
                result = String.Equals(sample, sampleOut);
                return result;
            }
    

    The problem what that you were not using the ASCIIEncoder to get the string back for sampleData.

    EDIT: Here’s a cleaned up version of the code to help with Closing/Disposing:

    public static bool Test()
            {
                string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
    
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    
                byte[] data = encoding.GetBytes(sample);
    
                // Compress.
                GZipStream hgs;
                byte[] cmpData;
    
                using(MemoryStream cmpStream = new MemoryStream())
                using(hgs = new GZipStream(cmpStream, CompressionMode.Compress))
                {
                    hgs.Write(data, 0, data.Length);
                    hgs.Close()
    
                    // Do this AFTER the stream is closed which sounds counter intuitive 
                    // but if you do it before the stream will not be flushed
                    // (even if you call flush which has a null implementation).
                    cmpData = cmpStream.ToArray();
                }  
    
                using(MemoryStream decomStream = new MemoryStream(cmpData))
                using(hgs = new GZipStream(decomStream, CompressionMode.Decompress))
                {
                    hgs.Read(data, 0, data.Length);
                }
    
                string sampleOut = encoding.GetString(data);
    
                bool result = String.Equals(sample, sampleOut);
                return result;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's something wrong with my code or I'm just not understanding this fully. I
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
This snippet of Perl code in my program is giving the wrong result. $condition
Whats wrong with the following c# code? Compiler reports this error: Inconsistent accessibility: parameter
Does anyone know what is wrong with this query? SELECT DISTINCT c.CN as ClaimNumber,
Does anyone have any idea what is wrong with this create statement for mysql?
I've the code shown below but nothing happens after execution system('mysqldump -u USER -pPASS
What's wrong with this query: INSERT INTO Users( weight, desiredWeight ) VALUES ( 160,
Am I doing something wrong is this a known issue with the ASP.NET MVC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.