我一次读取一个块的压缩数据,并将这些块添加到队列中。然后,当我尝试使用GzipStream解压缩该数据时,会显示InvalidDataException,表明GzipHeader中的幻数不正确。我的积木应该有什么标题?
这是我的减压代码:
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);
}
}
这是我的压缩代码:
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);
}
}
从文件中读取块:
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;
}
}
}