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

Top 5 Contributors of the Month

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);



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

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