.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 in one step

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

Serialize and Zip in one step
 

Once in a while it is needed that we need to serialize a particular object and zip it.  Here I am combining both the operations into one method.  The associated unit tests are also provided.

Source

public class SerializeNZip
{
    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();
    }

    public T UnzipAndDeserialize(string zipFileName)
    {
        FileStream compressedStream = new FileInfo(zipFileName).OpenRead();
        MemoryStream destinationStream = new MemoryStream();
        GZipStream gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
        byte[] buffer = new byte[4096];

        int numRead;

        while ((numRead = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            destinationStream.Capacity += numRead;
            destinationStream.Write(buffer, 0, numRead);
        }

        gzipStream.Dispose();
        compressedStream.Dispose();

        destinationStream.Position = 0;

        XmlSerializer deserializer = new XmlSerializer(typeof(T));
        T result = (T)deserializer.Deserialize(destinationStream);

        return result;
    }
}

Invoking

[TestMethod()]
public void UnzipAndDeserialize_List_Test()
{
    SerializeNZip target = new SerializeNZip();
    List list = new List();
    list.Add(new Person() { Id = 1, Name = "TestName1" });
    list.Add(new Person() { Id = 2, Name = "TestName2" });

    target.SerializeAndZip(list, "c:\\ser2.zip");

    List actual = target.UnzipAndDeserialize>("c:\\ser2.zip");

    Assert.IsNotNull(actual);
    Assert.AreEqual(2, actual.Count);
    Assert.AreEqual(1, actual[0].Id);
    Assert.AreEqual(2, actual[1].Id);
    Assert.AreEqual("TestName1", actual[0].Name);
    Assert.AreEqual("TestName2", actual[1].Name);
}

public class Person
{
    public int Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}


     

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