我不知道,如果任何人都可以阐明一些问题是推动我坚果:

我写的一个压缩解压测试类。来测试它,我将数据集中存储器流、压缩和解压它进行比较的结果。

压缩的现,但解压缩的是它在哪里打的污垢。这个是压缩职能:

    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.读返回0虽然仍有压缩的数据提取。

对于这些情况下,deflateStream.读取(s)只有一旦在做{}循环,然后返回一个压缩的流等于缓冲区大小,如果增加的缓冲区大小通过一个单一的字节的一切都很好,(除了失踪字节)。

输出,用于缓冲区大小的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

还注意到第3读的缓冲区大小65536例如:bytesRead:[58472]显然这也应该是65536有数据仍然留在缓冲区?

任何想法将会被 赞赏。

tia

  • 雅科
有帮助吗?

解决方案

你应该总是叫Close()上压缩的流。请注意,Flush()是 不够.我怀疑,因为这个放气流是缺失的数据。

其他提示

我的灵力告诉我你做的实际上有一个工作压实施,但是已经被遗忘的冲压缩的流。

好的我不能发现你的问题,而是遵循一些代码我写了一段时间前进 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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top