Decompress 和 Compress back 不 return 相同的内容

Decompress and Compress back does not return the same content

为什么在使用 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 ?

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

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

也许你的意思是

compressedBackBytes = compressor.ToArray();