压缩算法

  今天在无意中又找到一个压缩算法,他好象是.net自带的压缩算法.DeflateStream msdn上如何解释它:提供用于使用 Deflate 算法压缩和解压缩流的方法和属性。
结合前几天使用的标准zip压缩算法比较,这种方法压缩率没有zip压缩的高,不过他压缩的时间可是比zip快,或许这就是压缩率低的缘故.我在测试的时候都是以压缩字符为例,没有对图象或声音进行压缩处理.
 DeflateStream是在using System.IO.Compression;命名空间下,所以使用的时候需要导入这个命名空间.  
privatestaticbyte[] Compression(byte[] data, CompressionMode mode)
{
DeflateStream zip
=null;
try
{
if (mode == CompressionMode.Compress)
{
MemoryStream ms
=new MemoryStream();
zip
=new DeflateStream(ms, mode, true);
zip.Write(data,
0, data.Length);
zip.Close();
return ms.ToArray();
}

else
{
MemoryStream ms
=new MemoryStream();
ms.Write(data,
0, data.Length);
ms.Flush();
ms.Position
=0;
zip
=new DeflateStream(ms, mode, true);
MemoryStream os
=new MemoryStream();
int SIZE =1024;
byte[] buf =newbyte[SIZE];
int l =0;
do
{
l
= zip.Read(buf, 0, SIZE);
if (l ==0) l = zip.Read(buf, 0, SIZE);
os.Write(buf,
0, l);
}

while (l !=0);
zip.Close();
return os.ToArray();
}

}

catch
{
if (zip !=null) zip.Close();
returnnull;
}

finally
{
if (zip !=null) zip.Close();
}

}

此类表示 Deflate 算法,这是无损压缩和解压缩文件的行业标准算法。它结合了 LZ77 算法和霍夫曼编码。只能使用以前绑定的中间存储量来产生或使用数据,即使对于任意长度的、按顺序出现的输入数据流也是如此。这种格式可以通过不涉及专利使用权的方式轻松实现。有关更多信息,请参见 RFC 1951“ DEFLATE Compressed Data Format Specification version 1.3 ”(Deflate 压缩数据格式规范版本 1.3)。此类不能用于压缩大于 4 GB 的文件。(来自MSDN)

原文地址:https://www.cnblogs.com/lvfeilong/p/fgfgkfgk.html