本文介绍了GZipStream effectivness的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想大UINT16数组保存到一个文件中。 positionCnt为约50000,stationCnt是直接约2500保存,而不GZipStream,该文件是大约250MB其可以通过外部zip程序被压缩到19MB。用下面的代码文件为507MB。我该怎么办错了?



  GZipStream CMP =新GZipStream(File.Open(cacheFileName,FileMode.Create),CompressionMode.Compress ); 
的BinaryWriter FS =新的BinaryWriter(CMP);
fs.Write((Int32)已(positionCnt * stationCnt));
为(INT P = 0; P< positionCnt,P ++)
{
为(INT S = 0; S< stationCnt; S ++)
{
fs.Write(BoundData [p,S]);
}
}
fs.Close();


解决方案

不知道你正在运行的版本的.NET的上。在早期版本中,它使用的是尺寸,你从写缓冲区相同的窗口大小。因此,在你的情况下,它会尝试单独压缩每个整数。我的认为的他们改变了,在.NET 4.0,但尚未验证。



在任何情况下,你想要做的就是创建提前缓冲流 GZipStream



  GZipStream CMP =新GZipStream(File.Open(cacheFileName,FileMode.Create ),CompressionMode.Compress); 
BufferedStream buffStrm =新BufferedStream(CMP,65536);
的BinaryWriter FS =新的BinaryWriter(buffStrm);

这方式, GZipStream 获取数据64字节块,可以做压缩的一个更好的工作。



缓冲器大于64KB不会给你任何更好的压缩。


I am trying to save big UInt16 array into a file. positionCnt is about 50000, stationCnt is about 2500. Saved directly, without GZipStream, the file is about 250MB which can be compressed by external zip program to 19MB. With the following code the file is 507MB. What do I do wrong?

GZipStream cmp = new GZipStream(File.Open(cacheFileName, FileMode.Create), CompressionMode.Compress);
BinaryWriter fs = new BinaryWriter(cmp);
fs.Write((Int32)(positionCnt * stationCnt));
for (int p = 0; p < positionCnt; p++)
{
    for (int s = 0; s < stationCnt; s++)
    {
       fs.Write(BoundData[p, s]);
    }
}
fs.Close();
解决方案

Not sure what version of .NET you're running on. In earlier versions, it used a window size that was the same size as the buffer that you wrote from. So in your case it would try to compress each integer individually. I think they changed that in .NET 4.0, but haven't verified that.

In any case, what you want to do is create a buffered stream ahead of the GZipStream:

GZipStream cmp = new GZipStream(File.Open(cacheFileName, FileMode.Create), CompressionMode.Compress);
BufferedStream buffStrm = new BufferedStream(cmp, 65536);
BinaryWriter fs = new BinaryWriter(buffStrm);

This way, the GZipStream gets data in 64 Kbyte chunks, and can do a much better job of compressing.

Buffers larger than 64KB won't give you any better compression.

这篇关于GZipStream effectivness的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:49