.Net3.5中调用gzip压缩遇到的问题

日前,使用framework中的gzip进行数据压缩解压缩,遇到两个问题,着实费了些力气才找大,拿出来晒晒
问题一:解压后数据少两个byte?!
public   static   byte [] Compress( string  s)
        
{
            
byte[] buf = System.Text.Encoding.UTF8.GetBytes(s);
            MemoryStream ms 
= new MemoryStream();
            
byte[] rb;
            GZipStream gzip 
= new GZipStream(ms, CompressionMode.Compress, true);
            gzip.Write(buf, 
0, buf.Length);
            gzip.Flush();
            ms.Position 
= 0;
            rb 
= new byte[ms.Length];
            ms.Read(rb, 
0, (int)ms.Length);
           gzip.Close();
            ms.Close();

            
return rb;
        }
上面这段是一开始我使用的代码,基本正常,可是解压后总是短两个byte。
后来改为下面的代码,问题解决
正确的代码
public static byte[] Compress(string s)
        
{
            
byte[] buf = System.Text.Encoding.UTF8.GetBytes(s);
            MemoryStream ms 
= new MemoryStream();
            
byte[] rb;
            GZipStream gzip 
= new GZipStream(ms, CompressionMode.Compress, true);
            gzip.Write(buf, 
0, buf.Length);
            gzip.Flush();
            gzip.Close();
            ms.Position 
= 0;
            rb 
= new byte[ms.Length];
            ms.Read(rb, 
0, (int)ms.Length);
            ms.Close();
            
return rb;
        }
你发现了问题所在吗,对,就是读取之前需要先关闭GZipStream,从网上看到别人用Using,我试了试,也是不行的!
问题二:解压缩没有例外抛出,也不能读出数据!?
有问题的解压代码
public static string Decompress(byte[] buf)
        
{
            
long totalLength = 0;
            
int size = 0;
            MemoryStream ms 
= new MemoryStream(), msD = new MemoryStream();
            ms.Write(buf, 
0, buf.Length);
            ms.Seek(
0, SeekOrigin.Begin);
            GZipStream zip;
            zip 
= new GZipStream(ms, CompressionMode.Decompress);
            
byte[] db;
            
while (true)
            
{
                size 
= zip.ReadByte();
                
if (size != -1)
                
{
                    
                    totalLength
++;
                    msD.WriteByte((
byte)size);
                }

                
else
                
{
                    
break;
                }

            }

            zip.Close();
            db 
= msD.ToArray();
            msD.Close();
            
return System.Text.Encoding.UTF8.GetString(db);
        }
上面代码,无论我怎么执行调试,都不能正确解压,参数就是压缩函数的返回值!可是,意外发现如果调试在读取解压数据之前多停留一段时间,就可以读出数据!
正确执行的解压代码
public static string Decompress(byte[] buf)
        
{
            
long totalLength = 0;
            
int size = 0;
            MemoryStream ms 
= new MemoryStream(), msD = new MemoryStream();
            ms.Write(buf, 
0, buf.Length);
            ms.Seek(
0, SeekOrigin.Begin);
            GZipStream zip;
            zip 
= new GZipStream(ms, CompressionMode.Decompress);
            
byte[] db;
            
bool readed = false;
            
while (true)
            
{
                size 
= zip.ReadByte();
                
if (size != -1)
                
{
                    
if (!readed) readed = true;
                    totalLength
++;
                    msD.WriteByte((
byte)size);
                }

                
else
                
{
                    
if (readed) break;
                }

            }

            zip.Close();
            db 
= msD.ToArray();
            msD.Close();
            
return System.Text.Encoding.UTF8.GetString(db);
        }
大家看到,处于无奈增加一个标志,虽然问题解决了,但是总觉得不自在:),希望有高手指点一二!

时隔一年,终得巴山兄解惑,特此鸣谢!同样迷惑者请看巴山的回复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值