Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GZipStream Missing Byte

Status
Not open for further replies.

amarbirr

Programmer
Dec 26, 2010
7
0
0
CA
Hi,

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top