写一个从EXCEL里面读取照片路径
///
/// 解压文件
///
/// 压缩文件路径
/// 返回压缩文件夹路径
/// 解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
///
private bool UnZipFile(string zipFilePath, out string path, string unZipDir = null)
{
if (zipFilePath == string.Empty)
{
path = null;
return false;
}
if (!System.IO.File.Exists(zipFilePath))
{
path = null;
return false;
}
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (string.IsNullOrWhiteSpace(unZipDir))
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
try
{
using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = System.IO.File.Create(unZipDir + theEntry.Name))
{
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
{
path = null;
return false;
}
path = unZipDir;
return true;
}
}
在 while ((theEntry = s.GetNextEntry()) != null)的地方 s.GetNextEntry()) 会抛出 Unexpected EOF
有没有遇到过这个问题的可以分享一下解决方案
望采纳
从错误信息看来,你可能在尝试解压一个不完整或损坏的 zip 文件。
具体来说,当使用 ZipInputStream 的 GetNextEntry() 方法时,如果该方法返回 null,则表示已到达 zip 文件的末尾。如果在到达末尾之前就返回 null,则表示 zip 文件可能不完整或损坏。
如果你确信压缩文件是完整的,那么你可以通过添加异常处理的代码来避免这个错误,例如:
try {
// 解压缩代码
} catch (UnexpectedEOFException ex) {
// 处理异常
}
在这个例子中,如果在解压缩过程中遇到 Unexpected EOF 异常,程序就会进入 catch 块并执行相应的处理。