.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Serilization >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Serialize and Zip an object

Posted By:Jean Paul       Posted Date: January 01, 2011    Points: 15    Category: Serilization    URL: http://www.dotnetspark.com  

Serialize and Zip an object
 

public void SerializeAndZip(object obj, string zipFileName)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        MemoryStream stream = new MemoryStream();
        serializer.Serialize(stream, obj);
        stream.Position = 0;

        FileStream fileStream = File.Create(zipFileName);
        GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Compress);

        byte[] buffer = new byte[4096];
        int numRead;
        while ((numRead = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            gzipStream.Write(buffer, 0, numRead);
        }

        gzipStream.Dispose();
        fileStream.Dispose();
    }

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend