SharpZipLib - ZipException“System.ArgumentOutOfRangeException” - 为什么我会遇到这个异常?

发布于 2024-10-07 04:31:49 字数 2022 浏览 10 评论 0原文

我正在使用 SharpZipLib 来解压缩文件。我的代码对于所有 zip 文件都运行良好,除了我现在提取的 zip 文件...

得到这个异常:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: length

异常在 size = s.Read(data, 0, data.Length);< /code>

这是我的代码...

 public static void UnzipFile(string sourcePath, string targetDirectory)
     {
        try
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourcePath)))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    //string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    if (targetDirectory.Length > 0)
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(targetDirectory + fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error unzipping file \"" + sourcePath + "\"", ex);
        }
    }

I'm using SharpZipLib to unzip files. My code has been working nicely for all zipfiles except the zip file what i am extracting now...

Got this exception:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: length

The exception is being thrown at size = s.Read(data, 0, data.Length);

Hereb is my code...

 public static void UnzipFile(string sourcePath, string targetDirectory)
     {
        try
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourcePath)))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    //string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    if (targetDirectory.Length > 0)
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(targetDirectory + fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error unzipping file \"" + sourcePath + "\"", ex);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千と千尋 2024-10-14 04:31:49

对我来说看起来像一个错误。幸运的是,您可以访问代码,因此您应该能够准确地看到哪里出了问题。我建议您构建 SharpZipLib 的调试版本,中断引发异常的行,并查看它实际测试的内容。

即使没有剩余 2K 数据,读入 2K 缓冲区也应该没问题。

(我实际上不会按照您的方式编写代码,但这是另一回事。我还将其移至其自己的实用程序方法中 - 将所有数据从一个流复制到另一个流的行为非常常见。没有需要将其系在拉链上。)

Looks like a bug to me. Fortunately, you have access to the code, so you should be able to see exactly where it's going wrong. I suggest you build a debug version of SharpZipLib, break on the line which is throwing the exception, and have a look at what it's actually testing.

It should be fine to read into a 2K buffer even if there's not 2K of data left.

(I wouldn't actually write the code quite how you have, but that's a different matter. I'd also move it into its own utility method - the act of copying all the data from one stream to another is pretty common. There's no need to tie it to zip.)

浮光之海 2024-10-14 04:31:49

查看代码,您将再次读取同一组字节(并前进位置)。

size = s.Read(data, 0, data.Length);

来自 此处 显示第二个参数应该是移动位置 &不是一个固定的数字。

Looking at the code, you are reading the same set of bytes again (and advancing the position).

size = s.Read(data, 0, data.Length);

An example from here shows that the 2nd argument should be a moving position & not a fixed number.

‘画卷フ 2024-10-14 04:31:49

将代码 int size = 2048; 更改为 int size = data.Length;。您不会接受 OutOfRange 异常。

 using (FileStream streamWriter = File.Create(targetDirectory + fileName))
    {
       int size = data.Length;
       byte[] data = new byte[size];
       while (true)
       {
            size = s.Read(data, 0, data.Length);
            if (size > 0)
            {
                streamWriter.Write(data, 0, size);
            }
            else
            {
               break;
            }
       }
    }

Change your code int size = 2048; to int size = data.Length;. You won't take OutOfRange exception.

 using (FileStream streamWriter = File.Create(targetDirectory + fileName))
    {
       int size = data.Length;
       byte[] data = new byte[size];
       while (true)
       {
            size = s.Read(data, 0, data.Length);
            if (size > 0)
            {
                streamWriter.Write(data, 0, size);
            }
            else
            {
               break;
            }
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文