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

  • SEARCH
  • Home
  • 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

I've the code shown below but nothing happens after execution system('mysqldump -u USER -pPASS
I've always programmed in Java, which is probably why I'm so confused about this:
When I run the code below, Safari's debug console tells me: TypeError: Result of
I have included my source code draft below. I would appreciate any input on
Just want to make sure my test code below is proper. I've been reading
This is my fist stackoverflow post so please be gentle with me! I'm sure
Problem: How do you get a program to fail if a number goes beyond
How do I loop through the results of a SELECT statement in SQL? My
As long as I can remember, whenever I use a module I include a
I'm having a problem moving from a situation where an Outer Join works, to

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.