标准zip压缩

   这几天做了个TCP通讯的东西,由于需要发送的数据太大,所以想办法将发送的时候给他压缩,然后再发送压缩过的数据,接受方也可以减轻接受的时间和分配过大的内存空间造成的缓慢运行问题.我就将代码放下做个记号.

public class Compress
    
{
        
/**//// <summary>
        
/// 压缩大的byte数组
        
/// </summary>
        
/// <param name="uncompressedByte">没有压缩的byte数组</param>
        
/// <returns>压缩后的byte数组</returns>

  
        
public static byte[] Compression(byte[] uncompressedByte)
        
{
            MemoryStream ms 
= new MemoryStream();
            
using (Stream s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms))
            
{
                s.Write(uncompressedByte, 
0, uncompressedByte.Length);
                s.Close();
            }

            
byte[] compressedData = (byte[])ms.ToArray();
            
return compressedData;
        }

        
/**//// <summary>
        
/// 解压压缩的byte数组
        
/// </summary>
        
/// <param name="compressedByte">压缩的byte数组</param>
        
/// <param name="bufferLength">未压缩前的大小</param>
        
/// <returns>未压缩前的状态</returns>


        
public static byte[] DeCompression(byte[] compressedByte, int bufferLength)
        
{
            
byte[] bufferByte = new byte[bufferLength];
            
int totalLength = 0;
            
byte[] writeData = new byte[4096];
            
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
            
{
                
while (true)
                
{
                    
int size = s2.Read(writeData, 0, writeData.Length);
                    
if (size > 0)
                    
{
                        Buffer.BlockCopy(writeData, 
0, bufferByte, totalLength, size);
                        totalLength 
+= size;
                    }

                    
else
                    
{
                        
break;
                    }

                }

                s2.Close();
            }

            
return bufferByte;
        }

        
/**//// <summary>
        
/// 解压
        
/// </summary>
        
/// <param name="compressedByte">需要解压的数组</param>
        
/// <returns></returns>

        public static byte[] DeCompression(byte[] compressedByte)
        
{
            MemoryStream ms 
= new MemoryStream();
            
byte[] writeData = new byte[4096];
            
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
            
{
                
while (true)
                
{
                    
int size = s2.Read(writeData, 0, writeData.Length);
                    
if (size > 0)
                    
{
                        ms.Write(writeData, 
0, size);
                    }

                    
else
                    
{
                        
break;
                    }

                }

                s2.Close();
            }

            
return ms.ToArray();
        }
 
    }
使用他的时候我们需要添加第三方给我们提供的ICSharpCode.SharpZipLib.dll然后我们才可以使用,对字符的压缩比是很高的,我观察了下基本上可以压缩成100:1这样的效果.以后在遇见类似的情况可以参考下.

转载于:https://www.cnblogs.com/joyes/archive/2008/04/14/1153204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值