Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DotNetZip在特定文件上抛出错误_C#_.net_Dotnetzip - Fatal编程技术网

C# DotNetZip在特定文件上抛出错误

C# DotNetZip在特定文件上抛出错误,c#,.net,dotnetzip,C#,.net,Dotnetzip,好吧,所以我在玩弄DotNetZip,我想不出来 基本上,我将两个拉链合并在一起,前面的拉链覆盖后面的拉链。为此,我将使用以下代码从源zip中删除注入zip中的所有元素: foreach (ZipEntry entry in inZip) { if (outZip.ContainsEntry(entry.FileName)) { Log("Rem

好吧,所以我在玩弄DotNetZip,我想不出来

基本上,我将两个拉链合并在一起,前面的拉链覆盖后面的拉链。为此,我将使用以下代码从源zip中删除注入zip中的所有元素:

            foreach (ZipEntry entry in inZip)
            {
                if (outZip.ContainsEntry(entry.FileName))
                {
                    Log("Removing deprecated entry {0}", entry.FileName);
                    outZip.RemoveEntry(entry.FileName);
                }
            }

            outZip.Save();
然后我循环遍历注入zip中的所有元素,并暂时将它们保存到一个文件中,然后将它们重新添加到源zip中。像这样:

            foreach (ZipEntry entry in inZip)
            {
                if (!entry.IsDirectory)
                {
                    Log("Attempting to copy {0} to temp", entry.FileName);
                    string tempName = Path.Combine(tempPath, "tmp.tmp");
                    string oldName = entry.FileName;
                    byte[] buffer = new byte[65536];
                    Stream inStream = null;
                    FileStream stream = null;
                    try
                    {
                        Log("Reading from entry {0}", entry.FileName);
                        inStream = entry.OpenReader();
                        Log("Initializing new stream from path {0}", tempName);
                        stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
                        int size = 0;
                        Log("Prepping to read from stream");
                        while (
                            (size =
                            inStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            Log("Reading {0} bytes from entry {1}", buffer.Length, entry.FileName);
                            stream.Write(buffer, 0, size);
                        }

                        Log("Closing and flushing streams");
                        inStream.Close();
                        stream.Flush();
                        stream.Close();
                        Log("Opening read stream");
                        inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);

                        Log("Adding new entry");
                        outZip.AddEntry(oldName, inStream);
                    }
                    catch (Exception exe)
                    {
                        throw exe;
                    }
它工作得很好,直到我开始点击最初删除的文件。然后我得到以下错误:

Ionic.Zlib.ZlibException: Bad state (invalid stored block lengths)
有人能帮我找出问题所在吗?我已经把范围缩小到了while循环

                        while (
                            (size =
                            inStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            Log("Reading {0} bytes from entry {1}", buffer.Length, entry.FileName);
                            stream.Write(buffer, 0, size);
                        }

我想您需要
buffer.Length-1
。看起来您在缓冲区中读取的块太大了。Hmmm。没有任何变化