gpt4 book ai didi

c# - SharpZipLib - ZipException "System.ArgumentOutOfRangeException"- 为什么我会收到此异常?

转载 作者:太空狗 更新时间:2023-10-29 23:34:40 24 4
gpt4 key购买 nike

我正在使用 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);

处被抛出

这是我的代码...

 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);
}
}

最佳答案

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

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

(我实际上不会完全按照您的方式编写代码,但那是另一回事。我还将其移至其自己的实用方法中 - 将所有数据从一个流复制到另一个流的行为非常常见. 没有必要把它绑在 zipper 上。)

关于c# - SharpZipLib - ZipException "System.ArgumentOutOfRangeException"- 为什么我会收到此异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4436658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com