zoukankan      html  css  js  c++  java
  • ..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);
            }
  • 相关阅读:
    SQL第3课:具有约束的查询(第2部分)
    SQL第1课:SELECT查询
    idea快捷键
    Vue基础
    分布式基础
    数据结构-线性表
    常用算法
    数据结构-概述
    Django使用Jinja2模板引擎
    宿主机nginx使用容器php-fpm处理php请求
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2638479.html
Copyright © 2011-2022 走看看