GZipStream仅解压缩4kb数据

时间:2015-08-12 13:22:39

标签: c# gzip

为什么基于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变量无关)

1 个答案:

答案 0 :(得分:2)

您的gzip流实际上可能包含多个连接的gzip流。这是gzip格式允许的。 GZipStream可能只是阅读第一个。您需要做的就是对后续输入字节重复操作,直到消耗掉所有输入字节为止。