GZipStream丢失字节 [英] GZipStream Missing Byte

查看:57
本文介绍了GZipStream丢失字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解压缩压缩数据时,我缺少一个字节.
压缩过程如下:

I have a missing byte when i try to decompress, compressed data.
Compression is done as follows:

[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

推荐答案

我认为问题出在EOF或终止的空字符.只是预感,可能是因为数组大小不包含终止符,但是您的GZipStream需要添加EOF.
尝试在GZipStream上执行迭代WriteByte,然后关闭流.
如果不是这样,我将不得不尝试您的代码示例.
I think the problem is with EOF or the terminating null character. Just a hunch, it may be because you array size excludes the terminating character, but your GZipStream needs to add the EOF.
Try doing a iterative WriteByte on the GZipStream and then close the stream.
If it doesn''t I will have to try out your code example.


我在想这可能是字符串编码问题吗?
我正在从SQL Server读取数据以填充DataTable.
然后使用以下行将数据表写入XML:
I am thinking could it be string encoding issue?
I am reading data from SQL Server to fill up the DataTable.
Then datatable is written to XML with line:
<br />
MemoryStream tblStream = new MemoryStream( );<br />
tbl.WriteXml( tblStream );<br />
tblStream.Seek( 0, 0 );<br />


然后将其压缩并写入byte [].

我还注意到,当DataTable行中只有1行时,


This is then compressed and written to byte[].

I also noticed that when there is only 1 row in the DataTable lines

<br />
StreamReader reader = new StreamReader( gzip );<br />
lReturn = reader.ReadToEnd( );<br />


返回空字符串.即lReturn为空.我不明白为什么会这样.
我确实对此感到困惑.


return empty string. ie.e lReturn is empty. I can''t understand why that will be the case.
I am really stuck on this.


好吧,我进行了以下更改,以排除编码问题,并检查前后的输出字符串:
Ok I made following changes to rule out encoding as an issue and to check the strings out-put before and after:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.IO.Compression;
using System.Data;
using System.Text;
using System.Runtime.Serialization;

namespace S3.BillingModule
{
  /// <summary>
  /// Summary description for GzipDataTable
  /// </summary>

  [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.Position = 0;

      StreamReader tmpReader = new StreamReader( tblStream );
      string xml = tmpReader.ReadToEnd( );

      tblStream.Close( );
      tblStream.Dispose( );

      MemoryStream zipped = new MemoryStream( );
      GZipStream gzip = new GZipStream( zipped, CompressionMode.Compress );

      UTF8Encoding encoding = new UTF8Encoding( );
      byte[ ] bytes = encoding.GetBytes( xml );
      OrigSize = bytes.Length;

      gzip.Write( bytes, 0, bytes.Length );
      zipped.Position = 0;
      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 );

      MemoryStream msOutput = new MemoryStream( );
      byte[ ]   buffer = new byte[ 0x400 ];
      int count = gzip.Read( buffer, 0, buffer.Length );
      while( count != 0 )
      {
        msOutput.Write( buffer, 0, count );
        count = gzip.Read( buffer, 0, buffer.Length );
      }
      lReturn = Encoding.UTF8.GetString( msOutput.ToArray( ) );
      msOutput.Close( );
      gzip.Close( );
      return lReturn;

    }
  }
}


用法:


Usage:

//tbl is a System.Data.DataTables
GzipDataTable gzip = new GzipDataTable( tbl );
string whatever = gzip.Decompress( );



结果是:

1.进修之前



The results are:

1. Before Comrpession

<DocumentElement>
  <Employees>
    <emp_accpac_id>AMBSTE</emp_accpac_id>
    <emp_fname>STEPHENIA</emp_fname>
    <emp_lname>AMBONDEM</emp_lname>
    <cli_pdd_id>IFA003224014</cli_pdd_id>
    <cli_fname>Kathy</cli_fname>
    <cli_lname>Gillespie</cli_lname>
    <table_type>0</table_type>
    <pdd_auth_id>1</pdd_auth_id>
    <pdd_bill_code>1010H</pdd_bill_code>
    <pdd_bill_code_series>1000</pdd_bill_code_series>
    <billable>1</billable>
    <paycode>SIL</paycode>
    <rec_sched_id>32789</rec_sched_id>
    <emp_id>7</emp_id>
    <client_id>35</client_id>
    <auth_id>391</auth_id>
    <sched_date>2011-01-15T00:00:00-07:00</sched_date>
    <start_time>2011-01-15T09:00:00-07:00</start_time>
    <end_time>2011-01-15T16:00:00-07:00</end_time>
    <status>2</status>
    <created_by>79</created_by>
    <date_created>2010-12-07T13:29:26.48-07:00</date_created>
    <date_modified>2010-12-08T09:45:34.383-07:00</date_modified>
    <shift_type>1</shift_type>
  </Employees>
</DocumentElement>



压缩和解压后:



After Compression and Decompression:

<DocumentElement>
  <Employees>
    <emp_accpac_id>AMBSTE</emp_accpac_id>
    <emp_fname>STEPHENIA</emp_fname>
    <emp_lname>AMBONDEM</emp_lname>
    <cli_pdd_id>IFA003224014</cli_pdd_id>
    <cli_fname>Kathy</cli_fname>
    <cli_lname>Gillespie</cli_lname>
    <table_type>0</table_type>
    <pdd_auth_id>1</pdd_auth_id>
    <pdd_bill_code>1010H</pdd_bill_code>
    <pdd_bill_code_series>1000</pdd_bill_code_series>
    <billable>1</billable>
    <paycode>SIL</paycode>
    <rec_sched_id>32789</rec_sched_id>
    <emp_id>7</emp_id>
    <client_id>35</client_id>
    <auth_id>391</auth_id>
    <sched_date>2011-01-15T00:00:00-07:00</sched_date>
    <start_time>2011-01-15T09:00:00-07:00</start_time>
    <end_time>2011-01-15T16:00:00-07:00</end_time>
    <status>2</status>
    <created_by>79</created_by>
    <date_created>2010-12-07T13:29:26.48-07:00</date_created>
    <date_modified>2010-12-08T09:45:34.383-07:00</date_modified>
    <shift_type>1</shift_type>
  </Employees>
</DocumentEle



我在Visual Studio 2010调试器的Text Visualizer中看到了这些字符串.
注意压缩和解压缩后的< DocumentEle 版本.它应该是< DocumentElement> .
这让我发疯.有任何建议.



I am seeing these strings in Text Visualizer in Visual Studio 2010 Debugger.
Notice the <DocumentEle in version after compression and decompression. It should be <DocumentElement>.
This is driving me nuts. Any suggestions please.


这篇关于GZipStream丢失字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆