Some time we need to zip large DataSet to transfer over the http or any other system.
Following are the full code block to zip and unzip DataSet.
To use this code you need icsharpcode library, you can download it here.
Required Namespace:
Method to unzip DataSet:
Following are the full code block to zip and unzip DataSet.
To use this code you need icsharpcode library, you can download it here.
Required Namespace:
using System.IO; using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.GZip;
Method to zip DataSet:
public byte[] ZipDataset(DataSet data) { MemoryStream mem = new MemoryStream(); GZipOutputStream stream = new GZipOutputStream(mem); byte[] buffer; try { data.WriteXml(stream, XmlWriteMode.WriteSchema); stream.Finish(); mem.Seek(0, SeekOrigin.Begin); buffer = mem.ToArray(); return buffer; } catch { return null; } finally { mem.Close(); mem.Dispose(); stream.Close(); stream.Dispose(); }}
Method to unzip DataSet:
public DataSet UnzipDataset(byte[] buf) { DataSet data = new DataSet(); MemoryStream zStream = null; MemoryStream stream = new MemoryStream(); Stream outStream = null; int num; int count = 0x8000; try { zStream = new MemoryStream(buf); stream = new MemoryStream(); outStream = new GZipInputStream(zStream); byte[] output = new byte[count]; while ((num = outStream.Read(output, 0, count)) > 0) { stream.Write(output, 0, num); } stream.Seek(0L, SeekOrigin.Begin); data.ReadXml(stream, XmlReadMode.ReadSchema); return data; } catch { return null; } finally { stream.Close(); stream.Dispose(); outStream.Dispose(); outStream.Close(); zStream.Close(); zStream.Dispose(); } }
Download the reference library
No comments:
Post a Comment