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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:49:30+00:00 2026-06-10T08:49:30+00:00

I have a big object in memory which I want to save as a

  • 0

I have a big object in memory which I want to save as a blob into database.
I want to compress it before saving because database server is usually not local.

This is what I have at the moment:

using (var memoryStream = new MemoryStream())
{
  using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
  {
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(gZipStream, obj);

    return memoryStream.ToArray();
  }
}

However when I zip same bytes with Total Commander it cuts down the size always by 50% at least. With the above code it compresses 58MB to 48MB and anything smaller than 15MB gets even bigger.

Should I use a third-party zip library or is there a better way of doing this in .NET 3.5.
Any other alternatives to my problem?

EDIT:

Just found a bug in a code above. Angelo thanks for your fix.

GZipStream compression is still not great.
I gets Average 35% compression by gZipStream compared to TC 48% compression.

I have no idea what kind of bytes I was getting out with previous version 🙂

EDIT2:

I have found how to improve compression from 20% to 47%.
I had to use two Memory streams instead of one! Can anyone explain why is this the case?

Here is a code with 2 memory streams which does a lot better compression !!!

using (MemoryStream msCompressed = new MemoryStream())
using (GZipStream gZipStream = new GZipStream(msCompressed, CompressionMode.Compress))
using (MemoryStream msDecompressed = new MemoryStream())
{
  new BinaryFormatter().Serialize(msDecompressed, obj);
  byte[] byteArray = msDecompressed.ToArray();

  gZipStream.Write(byteArray, 0, byteArray.Length);
  gZipStream.Close();
  return msCompressed.ToArray();
}
  • 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-06-10T08:49:31+00:00Added an answer on June 10, 2026 at 8:49 am

    GZipStream from .NET 3.5 doesn’t allow you to set compression level. This parameter was introduced in .NET 4.5, but I don’t know if it will give you better result or upgrade is suitable for you.
    Built in algorithm is not very optimal, due to patents AFAIK.
    So in 3.5 is only one way to get better compression is to use third party library like SDK provided by 7zip or SharpZipLib. Probably you should experiment a little bit with different libs to get better compression of your data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a C# problem. I have a big object in memory at a
I have been trying to serialize a pretty big object. This object uses dictionaries
I have big URL list, which I have to download in parallel and check
I have big set of urls and I want to implement an autocompletion. I
I have this data which can be very big in size, this could be
I have a vector< Object > myvec which I use in my code to
I have an application, in which I want to displaying a number of image
Assume you have a reference counted object in shared memory. The reference count represents
Here's the situation: I have a massive object that needs to be loaded into
I have troubles finding a memory leak which then again causes my App 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.