为什么基于MemoryStream的GZipStream只能读取24576字节,而不能进一步读取?我用多线程对gz进行了部分压缩,但winrar可以解压缩,GZipStream不能。
static private bool ZipperWork2(string InputPath, string OutputPath, CompressionMode CompMode)
{
Mode = CompMode;
RawData = new byte[ThreadCount][];
CompressedData = new byte[ThreadCount][];
Thread[] CompressorThreads = new Thread[ThreadCount];
byte[] buffer = new byte[MAX_BLOCK_SIZE];
using (FileStream fs = new FileStream(InputPath, FileMode.Open))
{
using (FileStream fw = new FileStream(OutputPath, FileMode.Append))
{
while (fs.Position < fs.Length)
{
for (int i = 0; i < ThreadCount; i++)
{
int count = fs.Read(buffer, 0, MAX_BLOCK_SIZE);
RawData[i] = new byte[count];
Array.Copy(buffer, RawData[i], count);
CompressorThreads[i] = new Thread(WorkWithBlock2);
CompressorThreads[i].Start(i);
}
for (int i = 0; i < ThreadCount; i++)
{
CompressorThreads[i].Join();
fw.Write(CompressedData[i], 0, CompressedData[i].Length);
}
}
}
}
return true;
}
private static void WorkWithBlock2(object index)
{
int DataIndex = (int)index;
//int count=0,totalcount=0;
using (MemoryStream ms = new MemoryStream(RawData[DataIndex]))
{
using (MemoryStream output = new MemoryStream())
{
using (GZipStream gz = new GZipStream(ms, Mode))
{
gz.CopyTo(output);
}
CompressedData[DataIndex] = output.ToArray();
}
}
}我试着用不同的方法来做,但结果总是一样的,只解压缩了4KB (而且它与我的MAX_BLOCK_SIZE变量无关)
发布于 2015-08-12 22:49:13
您的gzip流实际上可能由多个串联的gzip流组成。这是gzip格式所允许的。GZipStream可能只读了第一个。您所需要做的就是在随后的输入字节上重复该操作,直到所有输入字节都用完为止。
https://stackoverflow.com/questions/31966510
复制相似问题