我想知道是否有人能对一个让我发疯的问题有所了解:
我正在编写压缩解压缩测试类。为了测试它,我将数据集序列化到一个内存流中,对其进行压缩,然后解压缩并比较结果。
压缩是很好的,但解压缩是它碰到污垢的地方。这是解压缩功能:
public static Stream GetUncompressedStreamCopy(Stream inStream)
{
Stream outStream = new MemoryStream();
inStream.Position = 0;
DeflateStream uncompressStream = new DeflateStream(inStream,
CompressionMode.Decompress, true);
byte[] buffer = new byte[65536];
int totalread = 0;
int bytesread = 0;
do {
bytesread = uncompressStream.Read(buffer, 0, buffer.Length);
totalread += bytesread;
outStream.Write(buffer, 0, bytesread);
Console.WriteLine("bytesRead: [{0}]\t outStream.Length [{1}]",
bytesread, outStream.Length);
} while (bytesread > 0);
Console.WriteLine("total bytes read [{0}]", totalread);
outStream.Flush();
return outStream;
}对于大小为65536的缓冲区,解压缩流返回的字节总是比未压缩的要少一个字节。
现在,我要谈谈我正在努力解决的第二个问题。对于某些缓冲区大小,uncompressStream.Read返回0,尽管仍有需要提取的压缩数据。
对于这些情况,deflateStream.Read(s)在do{}循环中只返回一次,然后返回等于缓冲区大小的未压缩流,如果将缓冲区大小增加一个字节,则一切正常(丢失字节除外)。
缓冲区大小为65536的输出:(原始未压缩数据为207833)
bytesRead: [65536] outStream.Length [65536]
bytesRead: [65536] outStream.Length [131072]
bytesRead: [58472] outStream.Length [189544]
bytesRead: [18288] outStream.Length [207832]
bytesRead: [0] outStream.Length [207832]
total bytes read [207832]缓冲区大小为189544 (一些神奇的数字,其中的代码坦克)
bytesRead: [189544] outStream.Length [189544]
bytesRead: [0] outStream.Length [189544]
total bytes read [189544]
Unompressed stream size 189544还请注意缓冲区大小65536 ex: bytesRead: 58472的第三次读取,这显然也应该是65536,因为缓冲区上还保留着数据?
任何想法都将受到极大的赞赏。
提亚
发布于 2009-10-22 14:29:23
您应该始终在压缩流上调用Close()。请注意()是不够的。我怀疑正是由于这个原因,排水流丢失了数据。
发布于 2009-10-22 14:25:24
我的心灵力量告诉我,你确实有一个工作的解压缩实现,但忘了冲洗压缩流之前。
发布于 2009-10-22 14:30:20
好吧,我找不到您的问题,但是请遵循我不久前为ICSharpCode.SharpZipLib编写的一些代码;
byte[] compressedData;
using(MemoryStream ms = new MemoryStream())
{
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
Stream s = new DeflaterOutputStream(ms, deflater);
s.Write(sendData, 0, sendData.Length);
s.Close();
compressedData = (byte[])ms.ToArray();
}
// ...
MemoryStream inflated = new MemoryStream();
using (Stream inflater = new InflaterInputStream(
inputStream, new Inflater(true)))
{
int count = 0;
byte[] deflated = new byte[4096];
while ((count = inflater.Read(deflated, 0, deflated.Length)) != 0)
{
inflated.Write(deflated, 0, count);
}
inflated.Seek(0, SeekOrigin.Begin);
}
byte[] content = new byte[inflated.Length];
inflated.Read(content, 0, content.Length);https://stackoverflow.com/questions/1607558
复制相似问题