I read compressed data one block at a time and add these blocks to the queue. Then when I try to decompress that data using GzipStream InvalidDataException is shown stating that the magic number in the GzipHeader is incorrect. What header should my blocks have?
Here's my decompression code:
private DataBlock Decompress(DataBlock sourceBlock)
{
var decompressed = new byte[sourceBlock.Data.Length];
using (MemoryStream memory = new MemoryStream(sourceBlock.Data))
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Decompress))
{
gzip.Read(decompressed, 0, sourceBlock.Data.Length);
}
return new DataBlock(decompressed, sourceBlock.Id);
}
}
Here's my compression code:
private DataBlock Compress(DataBlock sourceBlock)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress))
{
gzip.Write(sourceBlock.Data, 0, sourceBlock.Data.Length);
}
return new DataBlock(memory.ToArray(), sourceBlock.Id);
}
}
Read blocks from file:
private void ReadDataFromFile()
{
if (File.Exists(this.sourceFilePath))
{
try
{
using (var fsSource = new FileStream(this.sourceFilePath, FileMode.Open, FileAccess.Read))
{
while (fsSource.CanRead)
{
var buffer = new byte[this.bufferLength];
int n = fsSource.Read(buffer, 0, buffer.Length);
if (n == 0)
{
break;
}
var block = new DataBlock(buffer, this.currentReadBlockNumber++);
lock (this.innerDataBlockQueue)
{
innerDataBlockQueue.Enqueue(block);
}
this.newBlockDataLoaded?.Invoke(this, EventArgs.Empty);
SpinWait.SpinUntil(() => this.QueueIsFull(this.innerDataBlockQueue) == false);
}
}
}
catch (Exception e)
{
throw e;
}
}
}