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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:57:04+00:00 2026-05-17T02:57:04+00:00

I am having an issue with this test function where I take an in

  • 0

I am having an issue with this test function where I take an in memory string, compress it, and decompress it. The compression works great, but I can’t seem to get the decompression to work.

//Compress
System.IO.MemoryStream outStream = new System.IO.MemoryStream();                
GZipStream tinyStream = new GZipStream(outStream, CompressionMode.Compress);
mStream.Position = 0;
mStream.CopyTo(tinyStream);

//Decompress    
outStream.Position = 0;
GZipStream bigStream = new GZipStream(outStream, CompressionMode.Decompress);
System.IO.MemoryStream bigStreamOut = new System.IO.MemoryStream();
bigStream.CopyTo(bigStreamOut);

//Results:
//bigStreamOut.Length == 0
//outStream.Position == the end of the stream.

I believe that bigStream out should at least have data in it, especially if my source stream (outStream) is being read. is this a MSFT bug or mine?

  • 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-17T02:57:05+00:00Added an answer on May 17, 2026 at 2:57 am

    What happens in your code is that you keep opening streams, but you never close them.

    • In line 2, you create a GZipStream. This stream will not write anything to the underlying stream until it feels it’s the right time. You can tell it to by closing it.

    • However, if you close it, it will close the underlying stream (outStream) too. Therefore you can’t use mStream.Position = 0 on it.

    You should always use using to ensure that all your streams get closed. Here is a variation on your code that works.

    var inputString = "“ ... ”";
    byte[] compressed;
    string output;
    
    using (var outStream = new MemoryStream())
    {
        using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
        using (var mStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString)))
            mStream.CopyTo(tinyStream);
    
        compressed = outStream.ToArray();
    }
    
    // “compressed” now contains the compressed string.
    // Also, all the streams are closed and the above is a self-contained operation.
    
    using (var inStream = new MemoryStream(compressed))
    using (var bigStream = new GZipStream(inStream, CompressionMode.Decompress))
    using (var bigStreamOut = new MemoryStream())
    {
        bigStream.CopyTo(bigStreamOut);
        output = Encoding.UTF8.GetString(bigStreamOut.ToArray());
    }
    
    // “output” now contains the uncompressed string.
    Console.WriteLine(output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having this issue, but while trying to fix it as per the
I have this plugin issue for ages but can't get it right. Here is
We're having an issue with a poorly coded SQL function(which works fine in live,
I'm having an issue with stopping event propagation. Imagine this scenario: <table id=test> <tbody>
After having this issue on our websites over secure SSL connections for Office file
We are having this issue with our production servers (apache-tomcat-7.0.6), which is running a
I'm having this issue since offline_access has been deprecated and migration enabled. I'm getting
I'm having this issue where i use nth-child(odd) to color a table. In IE9
I am having this issue where Arabic letters are shown disjoint in any component
i'm having this issue, in ASP.NET MVC 2 where I'm adding a drop down

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.