关于System.IO.Compression.GZipStream的使用


       //添加引用

          using System.IO.Compression;

           
// 压缩数组
         public   unsafe   static   byte [] Compress( byte [] data)
        
{
            MemoryStream ms 
= new MemoryStream();
            Stream zipStream 
= new GZipStream(ms, CompressionMode.Compress, true);
            zipStream.Write(data, 
0, data.Length);
            zipStream.Close();


            
byte[] newByteArray = new byte[ms.Length];

            ms.Seek(
0, SeekOrigin.Begin);
            ms.Read(newByteArray, 
0, newByteArray.Length);

            
//zipStream.Write(buffer, 0, buffer.Length);
            
//zipStream.Read(newByteArray, 0, (int)ms.Length);


            
return newByteArray;
        }

        
// 解压缩数组
         public   unsafe   static   byte [] DeCompress( byte [] data)
        
{
            MemoryStream ms 
= new MemoryStream();

            ms.Write(data, 
0, data.Length);
            ms.Seek(
0, SeekOrigin.Begin);

            Stream zipStream 
= new GZipStream(ms, CompressionMode.Decompress,false);

            
byte[] newByteArray = RetrieveBytesFromStream(zipStream, 1);
            
return newByteArray;
        }

        
public   unsafe   static   byte [] RetrieveBytesFromStream(Stream stream,  int  bytesblock)
        
{
            
//---从一个流对象中检索字节---
            List<byte> lst = new List<byte>();
            
byte[] data=new byte[2];
            
int totalCount = 0;
            
try
            
{
                
while (true)
                
{
                    
//'---逐渐地增加数据字节数组-的大小--
                    int bytesRead = stream.Read(data, totalCount, bytesblock);
                    
if (bytesRead == 0)
                    
{
                        
break;
                    }

                    lst.Add(data[data.Length 
- 1]);
                    totalCount 
+= bytesRead;
                    data 
= new byte[totalCount+1];
                }

                
//'---确保字节数组正确包含提取的字节数---
                byte[]  data2 = new byte[totalCount];
                lst.CopyTo(data2, 
0);
                
                
                
return data2;
            }

            
catch (Exception ex)
            
{
                
//MsgBox(ex.ToString)
                return new byte[0];
            }

        }


private   void  button1_Click( object  sender, EventArgs e)
        
{
            
int len = 10*1024;
            
byte[] data = new byte[len];
            Random rnd 
= new Random(255);
            
for (int i = 0; i < len; i++)
            
{
                data[i] 
= (byte)(int)(i/45);
            }


            
int t1 = System.Environment.TickCount;
            
byte[] data2 = Compress(data);

            
//byte[] data3 = DeCompress(data2);
            int t2=System.Environment.TickCount;
            MessageBox.Show(
string.Format("耗时 {0} 耗秒.压缩比{1}/{2}={3}", t2 - t1, data2.Length, data.Length, data2.Length*1.0/data.Length));
        }

转载于:https://www.cnblogs.com/helper/articles/1229247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值