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 6017187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:05:33+00:00 2026-05-23T03:05:33+00:00

I built (based on a CodeProject article) a wrapper class (C#) to use a

  • 0

I built (based on a CodeProject article) a wrapper class (C#) to use a GZipStream to compress a MemoryStream. It compresses fine but doesn’t decompress. I’ve looked at many other examples that have the same problem, and I feel like I’m following what’s said but still am getting nothing when I decompress. Here’s the compression and decompression methods:

public static byte[] Compress(byte[] bSource)
{

    using (MemoryStream ms = new MemoryStream())
    {
        using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
        {
            gzip.Write(bSource, 0, bSource.Length);
            gzip.Close();
        }

        return ms.ToArray();
    }
}


public static byte[] Decompress(byte[] bSource)
{

    try
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress, true))
            {
                gzip.Read(bSource, 0, bSource.Length);
                gzip.Close();
            }

            return ms.ToArray();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("Error decompressing byte array", ex);
    }
}

Here’s an example of how I use it:

string sCompressed = Convert.ToBase64String(CompressionHelper.Compress("Some Text"));
// Other Processes
byte[] bReturned = CompressionHelper.Decompress(Convert.FromBase64String(sCompressed));
// bReturned has no elements after this line is executed
  • 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-23T03:05:34+00:00Added an answer on May 23, 2026 at 3:05 am

    There is a bug in Decompress method.

    The code does not read content of bSource. On the contrary, it overrides its content wile reading from empty gzip, created based on empty memory stream.

    Basically what your version of code is doing:

    //create empty memory
    using (MemoryStream ms = new MemoryStream())
    
    //create gzip stream over empty memory stream
    using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
    
    // write from empty stream to bSource
    gzip.Write(bSource, 0, bSource.Length);
    

    The fix could look like this:

    public static byte[] Decompress(byte[] bSource)
    {
        using (var inStream = new MemoryStream(bSource))
        using (var gzip = new GZipStream(inStream, CompressionMode.Decompress))
        using (var outStream = new MemoryStream())
        {
            gzip.CopyTo(outStream);
            return outStream.ToArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a custom-built Flash-based video player that I maintain, and it needs to
I built a template-based document generator using the Open XML SDK (1.0) , the
I have a custom built ajax [div] based dynamic dropdown. I have an [input]
We've built up an application infrastructure based on ActiveMQ. We can send and receive
I read somewhere that NTP is based on UDP and there's no security built
I remember coming across an article on I think CodeProject quite some time ago
I have an iphone app that I built based off a tutorial (for a
I am trying to create a dynamic table that is being built based on
We've built a javascript based widget that our client has embedded in their website.
I have an asp.net application in which I have built few silverlight based pages.

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.