SOURCES:
http://www.eggheadcafe.com/articles/20041128.aspURLURLURLusing System;
using System.Collections.Generic;
using System.Text;
namespace CompressDecompressDataSet
{
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
#endregion
//courtesy of Peter A. Bromberg, Ph.D.
namespace DSCompression
{
public class DataSetCodec
{
#region Main Function
static void Main(string[] args)
{
DataSetCodec dsc = new DataSetCodec();
DataSet ds = new DataSet();
ds.Tables.Add("Table");
ds.Tables["Table"].Columns.Add("include");
ds.Tables["Table"].Columns.Add("filledValues");
DataRow row = ds.Tables[0].NewRow();
row["include"] = true;
row["filledValues"] = "some value";
ds.Tables[0].Rows.Add(row);
Debugger.DebugDataSet("ok?", ref ds);
//now start compression
byte[] compressedDataSetInBytes = DataSetCodec.CompressDataSet(ref ds);
int lengthOfArray = compressedDataSetInBytes.Length ;
//NOW SHOW THE BYTES
foreach (byte b in compressedDataSetInBytes)
{
Debugger.WriteByte(b);
}
//now decompress
DataSetCodec.DecompressDataSet(ref compressedDataSetInBytes, out lengthOfArray );
Debugger.DebugDataSet("ok?", ref ds);
} //eof main
#endregion
// private ctor; all members are static
private DataSetCodec()
{
}
public static byte[] CompressDataSet(ref DataSet ds)
{
ds.RemotingFormat = SerializationFormat.Binary;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, ds);
byte[] inbyt = ms.ToArray();
System.IO.MemoryStream objStream = new MemoryStream();
System.IO.Compression.DeflateStream objZS =
new System.IO.Compression.DeflateStream(objStream,
System.IO.Compression.CompressionMode.Compress);
objZS.Write(inbyt, 0, inbyt.Length);
objZS.Flush();
objZS.Close();
return objStream.ToArray();
}
public static DataSet DecompressDataSet(ref byte[] bytDs, out int len)
{
System.Diagnostics.Debug.Write(bytDs.Length.ToString());
DataSet outDs = new DataSet();
MemoryStream inMs = new MemoryStream(bytDs);
inMs.Seek(0, 0);
DeflateStream zipStream = new DeflateStream(inMs, CompressionMode.Decompress, true);
byte[] outByt = ReadFullStream(zipStream);
zipStream.Flush();
zipStream.Close();
MemoryStream outMs = new MemoryStream(outByt);
outMs.Seek(0, 0);
outDs.RemotingFormat = SerializationFormat.Binary;
BinaryFormatter bf = new BinaryFormatter();
len = (int)outMs.Length;
outDs = (DataSet)bf.Deserialize(outMs, null);
return outDs;
}
public static byte[] ReadFullStream(Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
} //eof public static byte [] ReadFullStream
} //eof class
} //eof namespace DSCompression
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CompressDecompressDataSet
{
public class Debugger
{
public Debugger ( )
{
//
// TODO: Add constructor logic here
//
}
///
/// This method outputs on values of the passed dataset objects for easier debugging
/// ///
the string passed containng usual info
///
the dataset , which content one should debug
/// call it this way Utils.Debugger.DebugDataSet ( System.Reflection.MethodInfo.GetCurrentMethod().Name() , , ref ds );
public static void DebugDataSet ( string msg, ref System.Data.DataSet ds )
{
System.Diagnostics.Debug.WriteLine ( msg );
foreach (System.Data.DataTable dt in ds.Tables)
{
System.Diagnostics.Debug.WriteLine ( "================= Table " +
dt.TableName + " ========================= START" );
foreach (System.Data.DataColumn dc in dt.Columns)
{
System.Diagnostics.Debug.Write ( " | " );
System.Diagnostics.Debug.Write ( dc.ColumnName + " | " );
} //eof foreach (DataColumn dc in dt.Columns)
int rowNumber = 0;
foreach (System.Data.DataRow dr in dt.Rows)
{
System.Diagnostics.Debug.Write ( "\n row " + rowNumber + " --- " );
int colNumberInRow = 0;
foreach (System.Data.DataColumn dc in dt.Columns)
{
System.Diagnostics.Debug.Write ( " |" + colNumberInRow + "| " );
System.Diagnostics.Debug.Write ( dr [ dc ].ToString ( ) + " " );
colNumberInRow++;
} //eof foreach (DataColumn dc in dt.Columns)
rowNumber++;
} //eof foreach (DataRow dr in dt.Rows)
System.Diagnostics.Debug.Write ( " \n" );
System.Diagnostics.Debug.WriteLine ( "================= Table " +
dt.TableName + " ========================= END" );
} //eof foreach (DataTable dt in ds.Tables)
} //eof public static void DebugDataSet ( string msg , ref System.Data.DataSet ds )
public static void WriteLine ( string msg )
{
System.Diagnostics.Debug.WriteLine ( msg );
} //eof WriteLine
public static void Write ( string msg )
{
System.Diagnostics.Debug.Write ( msg );
} //eof Write
public static void WriteByte(byte b)
{
System.Diagnostics.Debug.Write(b.ToString());
} //eof WriteByte
} //eof class Debugger
} //eof namespace