zoukankan      html  css  js  c++  java
  • 压缩和解压缩数据流

      //压缩压缩后的字节数组
            public byte[] Compress(byte[] data)
            {
                MemoryStream ms 
    = new MemoryStream();
                Stream zipStream 
    = new GZipStream(ms, CompressionMode.Compress, true);
                zipStream.Write(data, 
    0, data.Length);
                zipStream.Close();
                ms.Position 
    = 0;
                
    byte[] buffer = new byte[ms.Length];
                ms.Read(buffer, 
    0,int.Parse(ms.Length.ToString()));
                
    return buffer;
            }

    //解压缩
      public static byte[] Decompress(byte[] data)
            {
                
    try
                {
                    MemoryStream ms 
    = new MemoryStream(data);
                    Stream zipStream 
    = null;
                    zipStream 
    = new GZipStream(ms, CompressionMode.Decompress);
                    
    byte[] dc_data = null;
                    dc_data 
    = EtractBytesFormStream(zipStream, data.Length);
                    
    return dc_data;
                }
                
    catch
                {
                    
    return null;
                }
            }


            
    public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
            {
                
    try
                {
                    
    byte[] data = null;
                    
    int totalBytesRead = 0;
                    
    while (true)
                    {
                        Array.Resize(
    ref data, totalBytesRead + dataBlock + 1);
                        
    int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
                        
    if (bytesRead == 0)
                        {
                            
    break;
                        }
                        totalBytesRead 
    += bytesRead;
                    }
                    Array.Resize(
    ref data, totalBytesRead);
                    
    return data;
                }
                
    catch
                {
                    
    return null;
                }
            }
  • 相关阅读:
    关于 var YAHOO = window.YAHOO || {}; 的解释
    javacsript 上传文件(与websrvice对接)原创
    Jquery ajax参数设置
    分页存储过程(对有主键的表效率极高) ,以及在asp.net中配合LtpPageControl的用法
    一些常用的dos命令
    SQL字符串函数
    (转)UML建模风格之状态图概要
    (转)也谈设计模式,实例票据打印 解析 Decorator
    (转)UML建模风格之状态图详述
    JQuery find方法Bug
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1541062.html
Copyright © 2011-2022 走看看