CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Serialization

  1. #1
    Join Date
    Apr 2003
    Location
    Copenhagen, Denmark
    Posts
    6

    Serialization

    Hi all,

    Is there a way to compress files that is the result of serialized objects?

    Regards
    Peter

  2. #2
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Hm.. Sure it is possible...

    Generally, there are two ways how to do this kind of serialization.
    A.) Implement ISearializable interface in the classes you want searialize. By this method you can implement custom serialization process.

    B.) Explicitelly compress the file produced by basic serialization process. You have to explicitelly compress the file after you perform serialization of all classes you want store on the disk. Additionally, before you deserialize them, you need to decompress that file.

    Method A is more "straightforward" however you can discover this method as not enough effective for you. It is due to the phylosophy of compressing algorithm. You can just compress individual fields of the class (or event one instance of the class) using A method. However, this means the degradation of effectiveness of compress algorithms. You can compare it with following:

    You have one file of size 1GB. When you compress this file, you can decrease its size for example to 0.5GB.

    Now, try to divide 1GB file into 10 0.1GB files. Compress individually each of these new files and the bind them together. Size of the resulting file will be greater then 0.5GB. It means, it is degradation of compression process.

    This is similiar to situation that you implement your custom serialization (Method A). Method A is much less effective than method B.

    Method B is not so "clear" and needs additional logic to be implemented after serialization and before deserialization. However the resulting disk space save will be really better as when you use method A.

    The another possibility is to embedd all objects that you want to serialize into one class. Make them members of that class. Then make custom serialization only for that class. In GetObjectData() method of that class just use basic serialization for all members of the class, and resulting data compress before you add them into the SerializationInfo object of "supperior" class. The same way use in serialization contructor of your class.

    Implementation of method A:
    Code:
    [Serializable]
    public class YourObject : ISerializable
    {
        public string strHugeString1;
        public string strHugeString2;
    
        // Public contructor used for object creation
        public YourObject()
        {
        }
    
        // Serialization contructor. Will be used during deserialization
        public YourObject(SerializationInfo info, StreamingContext context)
        {
            strHugeString1 = DecompressString(info.GetString("s1"));
            strHugeString2 = DecompressString(info.GetString("s2"));
        }
    
        // The member is called during serialization process to get data to be serialized...
        // You can use context to determine kind of serialization and for example disallow 
        // serialization of things like window handles or references to created com objects.
        public void GetObjectData(SearializationInfo info, StreamingContext context)
        {
            info.AddValue("s1", CompressString(strHugeString1));
            info.AddString("s2", CompressString(strHugeString2));
        }
    
        // Implement algorithm to decompress string
        protected string DecompressString(string strCompressed)
        {
        }
    
        // Implement algorithm to compress string
        protected string CompressString(string strPlain)
        {
        }
    }
    martin

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    you can compress the serialized files however you will need to encorporate third party or Microsoft's cab compressor in your program and have that file compressed.

    -Paresh
    - Software Architect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured