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

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);
        }
原文地址:https://www.cnblogs.com/zpc870921/p/2638479.html