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


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Serialize and Zip an object

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

Usage: SerializeAndZip("test data", "c:\\file.zip");
 

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


private void Compress(Stream inStream, string destinationFile)
        {
            FileStream fileStream = File.Create(destinationFile);
            GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Compress);

            byte[] buffer = new byte[4096];
            int numRead;
            while ((numRead = inStream.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
Here

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