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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:03:56+00:00 2026-05-13T12:03:56+00:00

I want to Compress an Object in dot net to reduce its size and

  • 0

I want to Compress an Object in dot net to reduce its size and then UnCompress it on in my client application.

Thanks,
Mrinal Jaiswal

  • 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-13T12:03:56+00:00Added an answer on May 13, 2026 at 12:03 pm

    I have update the code there was a problem with older version.

    Here is a function which serialize and compress and viceversa.

    public static byte[] SerializeAndCompress(object obj) {
    
        using (MemoryStream ms = new MemoryStream()) {
            using (GZipStream zs = new GZipStream(ms, CompressionMode.Compress, true)) {
                BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(zs, obj);
            }
            return ms.ToArray();
        }
    }
    public static object DecompressAndDeserialze(byte[] data) {
    
        using (MemoryStream ms = new MemoryStream(data)) {
            using (GZipStream zs = new GZipStream(ms, CompressionMode.Decompress, true)) {
                BinaryFormatter bf = new BinaryFormatter();
                return bf.Deserialize(zs);
            }
        }
    }
    

    Following is how to use it.

        [Serializable]
        class MyClass
        {
            public string Name { get; set; }
        }
    
        static void Main(string[] args) {
            MyClass myClassInst = new MyClass();
            myClassInst.Name = "Some Data";
    
            byte[] data= SerializeAndCompress(myClassInst);
            MyClass desInst = (MyClass)DecompressAndDeserialze(data);
    
        }
    

    But there is a catch to compression. Remember the above example object is serialize to 153 bytes but the compress version is 266 bytes the reason is that if have small objects with less data then the gzip header information and compression header will at least take 120bytes. So if your object are big enough than compress them if they are just less 300 bytes or so its no need to compress them. You can check compression ratio and see if you object even require compression.

    Another suggestion try to compress bulk of data will always give better compression over individual compress objects.

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

Sidebar

Related Questions

I have an Umbraco 4.7.x project (.NET 4.0 webforms) where I want to compress
I want to compress and then encrypt my data, and for improved speed (by
I want to compress and encrypt my whole application, maybe with Zip, 7z, Bz2,
I want to compress responses coming from my IIS Express driven web application. We're
I want to compress the viewstate. Therefore I need to override SavePageStateToPersistenceMedium wich belongs
here is my original function. it works perfectly, I just want to compress it
I want to use YUI Compressor to combine and compress my css and js
Want the function to sort the table by HP but if duplicate HPs then
I want to compress a file before saving physically on the disk. I tried
I want to compress an NSString. How I can do that in objective-c (iphone).

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.