XCeed Streaming Compression in C# with COM Interop

If you have been using XCeedsoft's Streaming Compression Library in classic ASP and want to now port that site over to ASP.NET, you'll need to use COM Interop to compress and decompress your data.

If you have been using XCeedsoft's Streaming Compression Library in classic ASP and want to
now port that site over to ASP.NET, you'll need to use COM Interop to compress and decompress
your data. At the time of this writing, XCeedsoft did not have a .NET version of their Streaming Compression
Library.

The following is a code sample showing how to query a binary or image datatype column from
SQL Server and decompress the contents into a string in C#. There are several compression
types available. I just happen to use BTW.

public bool XmlLoadHierarchy()
{

bool fRet=false;
string sXML="";
object oCompXML=null;

SqlConnection oConn = new SqlConnection();
XmlDocument oXML = new XmlDocument();

try
{
oConn.ConnectionString = "your connection string";
oConn.Open();

SqlCommand oCmd = new SqlCommand("your stored procedure",oConn);
SqlDataReader oReader = oCmd.ExecuteReader();

while (oReader.Read())
{
byte[] objectByteArray = (byte[])oReader["BINARYCOLUMNNAME"];
oCompXML = (object)objectByteArray;
}

oReader.Close();
oConn.Close();

sXML = this.XMLDecompress(ref oCompXML);

if (sXML !="")
{
oXML.LoadXml(sXML);
fRet=true;
}
}
catch (Exception) { }
finally
{
if (oConn.State == ConnectionState.Open) { oConn.Close(); }
}

return fRet;
}

public string XMLDecompress(ref object oCompXML)
{
bool fLicense;
string sXML="";
object oUnCompressed;

try
{

XceedStreamingCompressionLib.XceedStreamingCompressionClass oComp = new XceedStreamingCompressionLib.XceedStreamingCompressionClass();

fLicense = oComp.License("your license key");

XceedStreamingCompressionLib.XceedBWTCompressionMethodClass oBWT = new XceedStreamingCompressionLib.XceedBWTCompressionMethodClass();

oComp.CompressionFormat = oBWT;

oUnCompressed = oComp.Decompress(ref oCompXML,true);

sXML = System.Text.Encoding.Unicode.GetString((byte[])oUnCompressed);

}
catch (Exception) { }

return sXML;

}


Submission Date:  9/23/2005 2:54:06 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

By Robbe Morris   Popularity  (504 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP