0

为什么我在使用 DeflateStream 解压和压缩回字节数组后没有得到相同的内容?

编码:

byte[] originalcontent = Same Byte Array Content
byte[] decompressedBytes;
byte[] compressedBackBytes;

// Decompress the original byte-array
using (Stream contentStream = new MemoryStream(originalcontent, false))
using (var zipStream = new DeflateStream(contentStream, CompressionMode.Decompress))
using (var decStream = new MemoryStream())
{
    zipStream.CopyTo(decStream);
    decompressedBytes = decStream.ToArray();
}

// Compress the byte-array back
using (var input = new MemoryStream(decompressedBytes, true))
using (var compressStream = new MemoryStream())
using (var compressor = new DeflateStream(compressStream, CompressionMode.Compress))
{
    input.CopyTo(compressor);
    compressedBackBytes = compressStream.ToArray();
}

为什么 originalcontent != compressedBackBytes ?

4

1 回答 1

1

看起来您所做的一切都正确,直到您获取原始输入流并覆盖了包含解压缩字节的压缩器。您需要将压缩器字节放入compressedBackBytes。

您的输入(从解压缩开始)似乎将解压缩的字节复制到其中;然后你将它复制到压缩器,它会覆盖你刚刚解压缩的内容。

也许你的意思是

compressedBackBytes = compressor.ToArray();
于 2018-09-05T18:32:21.207 回答