首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GZipStream仅解压缩4kb的数据

GZipStream仅解压缩4kb的数据
EN

Stack Overflow用户
提问于 2015-08-12 21:22:39
回答 1查看 518关注 0票数 0

为什么基于MemoryStream的GZipStream只能读取24576字节,而不能进一步读取?我用多线程对gz进行了部分压缩,但winrar可以解压缩,GZipStream不能。

代码语言:javascript
运行
复制
        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变量无关)

EN

回答 1

Stack Overflow用户

发布于 2015-08-12 22:49:13

您的gzip流实际上可能由多个串联的gzip流组成。这是gzip格式所允许的。GZipStream可能只读了第一个。您所需要做的就是在随后的输入字节上重复该操作,直到所有输入字节都用完为止。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31966510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档