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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:28:05+00:00 2026-05-20T22:28:05+00:00

Say I have an object called data which contains a variety of information. Let’s

  • 0

Say I have an object called data which contains a variety of information. Let’s say for argument that there is actually quite a lot of stuff within the data graph.

If I serialise it using BinaryFormatter then I get a file which is, say, 5Mb.
If I encapsulate the serialisation stream in a GZipStream then I get a much smaller file, say, 1Mb.

I can, if I want, encrypt the stream while compressing it, or encrypt the stream without compressing it.

The issue is: I need to know what was done during serialisation so that I know what to do when I deserialise it.

One technique would be to use a different file extension. For example, an uncompressed, unencrypted file might have a .dat extension, .zdat for compressed, .cdat for encrypted, and .czdat for compressed and encrypted.

This would work, but it introduces a potential problem: What if the user changes the extension, etc. It also means that if I want to associate the files in Windows, there are 4 extensions instead of 1 which need to be associated – quadrupling the risk of collisions with existing the associations.

If I wrap my data object in a simple class:

[Serializable]
public class SerialisationContainer
{
   public string SerialisedData { get; private set; }

   public bool Compressed { get; private set; }
   public bool Encrypted { get; private set; }

   public SerialisationContainer()
   {
     // etc...
   }

   public object GetObject()
   {
     // etc...
   }
}

then I’m basically serialising an object which has a serialised stream in it which may be compressed and/or encrypted, but we don’t know or care at this point because the meta-information is stored in the SerialisationContainer.

What do you think? I’m basically just curious what you think of this method, and what you do in similar situations. I think the above method is a very wasteful way of doing what I want. I would basically need to serialise my data graph to a memory stream, convert it to a string, place the string inside my container, and then serialise it again.

Another issue is the length of the string SerialisedData. In the example I gave we only have about 5Gb of BinaryData, but what about when it starts getting larger? I know an upper-bound for a string on a 64-bit OS is around 2GB and significantly less for a 32-bit OS. Do streams have such a limitation? Since streams are written in blocks of bytes, it makes sense that they wouldn’t.

  • 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-20T22:28:06+00:00Added an answer on May 20, 2026 at 10:28 pm

    First of all, the lazy solution: you don’t have to serialize directly to a file. You can serialize to memory, and then write a file that has 1 byte for format followed by serialization data.

    Second, you can get a little smarter: Open a file; write one byte to it (the format); serialize into the same string. To deserialize, read one byte to figure out the format, and then pass the stream to the deserializer; it will only read data after that one byte.

    If you have the methods

    void SerializeToStream(Stream stream, bool compress, bool encrypt);
    void DeserializeFromStream(Stream stream, bool compressed, bool encrypted);
    

    your code can look like this:

    // Could also use a flags enum for these
    const int EncryptBit = 1;
    const int CompressBit = 2;
    
    public void SaveToFile(string filename, bool compress, bool encrypt) {
        byte format = (byte)((compress ? CompressBit : 0) | (encrypt ? EncryptBit : 0));
        using (Stream stream = File.OpenWrite(filename)) {
            stream.WriteByte(format);
            SerializeToStream(stream, compress, encrypt);
        }
    }
    
    public void LoadFromFile(string filename) {
        using (Stream stream = File.OpenRead(filename)) {
            int format = stream.ReadByte();
            if (format < 0 || format >= 4) {
                throw new InvalidOperationException("Unknown file format");
            }
    
            bool compressed = format & CompressBit != 0;
            bool encrypted = format & EncryptBit != 0;
            DeserializeFromStream(stream, compressed, encrypted);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data object (let's say it's called 'Entry') that has a set
I have an object called Result<T> that has a constructor which accepts an argument
Let's say we have a object called a Widget, for which we can construct
Say I have a model object 'Person' defined, which has a field called 'Name'.
Let's say I have a java.util.Properties object. The Properties object has a method called
So lets say I have a class called Post that contains an IList I
Say I have an object-like data record like this: $article = array( 'title' =>
Lets say I have an object that has stringProp1, stringProp2. I wish to store
Let's say you have an object literal: var d = { x: +'35', y:
Let's say I have a GridViewEx class declared which extends GridView . And inside

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.