Why don't I get the same content after decompressing and compressing-back a byte-array with DeflateStream?
The code:
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();
}
Why originalcontent != compressedBackBytes ?
It looks like you did everything properly, until you took the original input stream and overwrote your compressor, which contains your decompressed bytes. You need to place your compressor bytes into compressedBackBytes.
Your input (beginning with the decompress) seems it copies the decompressed bytes into it; then later you copy it to the compressor, which overwrites what you just decompressed.
Maybe you meant something like
compressedBackBytes = compressor.ToArray();