Hi,
I have a missing byte when i try to decompress, compressed data.
Compression is done as follows:
Usage:
I have a missing byte when i try to decompress, compressed data.
Compression is done as follows:
Code:
[DataContract]
public class GzipDataTable
{
[DataMember]
public byte[ ] CompressedBytes;
private long OrigSize;
private long CompressedSize;
public GzipDataTable( DataTable tbl )
{
MemoryStream tblStream = new MemoryStream( );
tbl.WriteXml( tblStream );
tblStream.Seek( 0, 0 );
MemoryStream zipped = new MemoryStream( );
GZipStream gzip = new GZipStream( zipped, CompressionMode.Compress );
byte[ ] bytes = tblStream.ToArray( );
OrigSize = bytes.Length;
gzip.Write( bytes, 0, bytes.Length );
CompressedBytes = zipped.ToArray( );
CompressedSize = CompressedBytes.Length;
gzip.Close( );
}
public string Decompress( )
{
string lReturn = string.Empty;
MemoryStream cprsStream = new MemoryStream( );
cprsStream.Write( CompressedBytes, 0, CompressedBytes.Length );
cprsStream.Position = 0;
GZipStream gzip = new GZipStream( cprsStream, CompressionMode.Decompress );
StreamReader reader = new StreamReader( gzip );
lReturn = reader.ReadToEnd( );
long length = lReturn.Length;
gzip.Close( );
return lReturn;
}
private string BytesToString( byte[ ] buff, int length )
{
string content = string.Empty;
for( int i = 0; i < length; i++ )
{
content += Convert.ToString( ( char )buff[ i ] );
}
return content;
}
}
Usage:
Code:
//assume tbl is a System.Data.DataTable with 100 rows
GzipDataTable gzip = new GzipDataTable( tbl );
string blah = gzip.Decompress( );
//blah is one byte less then original size of uncompressed //XML string.
//The result is it's missing closing ">" for the root tag, hence
//xml parse barfs