今天在无意中又找到一个压缩算法,他好象是.net自带的压缩算法.DeflateStream msdn上如何解释它:提供用于使用 Deflate 算法压缩和解压缩流的方法和属性。
结合前几天使用的标准zip压缩算法比较,这种方法压缩率没有zip压缩的高,不过他压缩的时间可是比zip快,或许这就是压缩率低的缘故.我在测试的时候都是以压缩字符为例,没有对图象或声音进行压缩处理.
DeflateStream是在using System.IO.Compression;命名空间下,所以使用的时候需要导入这个命名空间.
private static byte[] 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 = new byte[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();
return null;
}
finally
{
if (zip != null) zip.Close();
}
}
结合前几天使用的标准zip压缩算法比较,这种方法压缩率没有zip压缩的高,不过他压缩的时间可是比zip快,或许这就是压缩率低的缘故.我在测试的时候都是以压缩字符为例,没有对图象或声音进行压缩处理.
DeflateStream是在using System.IO.Compression;命名空间下,所以使用的时候需要导入这个命名空间.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
此类表示 Deflate 算法,这是无损压缩和解压缩文件的行业标准算法。它结合了 LZ77 算法和霍夫曼编码。只能使用以前绑定的中间存储量来产生或使用数据,即使对于任意长度的、按顺序出现的输入数据流也是如此。这种格式可以通过不涉及专利使用权的方式轻松实现。有关更多信息,请参见 RFC 1951“