Peanutssprouted 2022-12-12 11:25 采纳率: 12.5%
浏览 66
已结题

EXCEL导入图片错误

写一个从EXCEL里面读取照片路径

/// 
        /// 解压文件
        /// 
        /// <param name="zipFilePath">压缩文件路径</param>
        /// <param name="path">返回压缩文件夹路径</param>
        /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
        /// <returns></returns>
        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 
有没有遇到过这个问题的可以分享一下解决方案
  • 写回答

1条回答 默认 最新

  • ShowMeAI 2022-12-12 13:36
    关注

    望采纳


    从错误信息看来,你可能在尝试解压一个不完整或损坏的 zip 文件。

    具体来说,当使用 ZipInputStream 的 GetNextEntry() 方法时,如果该方法返回 null,则表示已到达 zip 文件的末尾。如果在到达末尾之前就返回 null,则表示 zip 文件可能不完整或损坏。

    如果你确信压缩文件是完整的,那么你可以通过添加异常处理的代码来避免这个错误,例如:

    try {
    // 解压缩代码
    } catch (UnexpectedEOFException ex) {
    // 处理异常
    }
    

    在这个例子中,如果在解压缩过程中遇到 Unexpected EOF 异常,程序就会进入 catch 块并执行相应的处理。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀