C# Zip文件下载解压报错:System.IO.InvalidDataException求助
问题:下载Zip文件后解压触发System.IO.InvalidDataException,提示“End of Central Directory record could not be found”
我在使用C#代码下载并解压Zip文件时遇到问题,触发System.IO.InvalidDataException异常,错误提示为“End of Central Directory record could not be found”。一开始以为是Google Drive的问题,换成Dropbox后问题依旧。
以下是我使用的代码:
private void InstallGameFiles(bool _isUpdate, Version _onlineVersion) { try { WebClient webClient = new WebClient(); if (_isUpdate) { Status = LauncherStatus.downloadingUpdate; } else { Status = LauncherStatus.downloadingGame; _onlineVersion = new Version(webClient.DownloadString("https://www.dropbox.com/scl/fi/9wc1btx28iz03rr3r6gkt/Version.txt?rlkey=67mgp885v82m9qdafan1se469&st=h7lchvjt&dl=0")); } webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadGameCompletedCallback); webClient.DownloadFileAsync(new Uri("https://www.dropbox.com/scl/fi/3evjysl179fjrnvxx9uu8/Build.zip?rlkey=c1rcmm7qd0n95whwmc2c2inos&st=yntg8128&dl=0"), gameZip, _onlineVersion); } catch (Exception ex) { Status = LauncherStatus.failed; MessageBox.Show($"Error installing game files: {ex}"); } } private void DownloadGameCompletedCallback(object sender, AsyncCompletedEventArgs e) { try { string onlineVersion = ((Version)e.UserState).ToString(); ZipFile.ExtractToDirectory(gameZip, rootPath, true); File.Delete(gameZip); File.WriteAllText(versionFile, onlineVersion); VersionText.Text = onlineVersion; Status = LauncherStatus.ready; } catch (Exception ex) { Status = LauncherStatus.failed; MessageBox.Show($"Error downloading: {ex}"); } }
我原本期望Zip文件能正常下载并解压,已尝试使用Windows默认Zip系统,还更换了存储平台,但仍持续出现该错误。
问题根源
你使用的Dropbox链接参数为dl=0,这个参数会返回网页预览页面,而非直接下载Zip文件本身。你实际下载的是一个HTML文件,自然无法被识别为合法的Zip格式,因此触发解压错误。
修复步骤
- 修改下载链接参数:将所有Dropbox链接末尾的
dl=0改为dl=1,这是Dropbox的直接下载参数。 - 同步修复Version.txt链接:获取版本号的
Version.txt链接同样使用了dl=0,需一并修改,否则会下载HTML页面导致版本解析失败。
修复后的代码示例
private void InstallGameFiles(bool _isUpdate, Version _onlineVersion) { try { WebClient webClient = new WebClient(); if (_isUpdate) { Status = LauncherStatus.downloadingUpdate; } else { Status = LauncherStatus.downloadingGame; // 修改Version.txt链接的dl参数为1 _onlineVersion = new Version(webClient.DownloadString("https://www.dropbox.com/scl/fi/9wc1btx28iz03rr3r6gkt/Version.txt?rlkey=67mgp885v82m9qdafan1se469&st=h7lchvjt&dl=1")); } webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadGameCompletedCallback); // 修改Build.zip链接的dl参数为1 webClient.DownloadFileAsync(new Uri("https://www.dropbox.com/scl/fi/3evjysl179fjrnvxx9uu8/Build.zip?rlkey=c1rcmm7qd0n95whwmc2c2inos&st=yntg8128&dl=1"), gameZip, _onlineVersion); } catch (Exception ex) { Status = LauncherStatus.failed; MessageBox.Show($"Error installing game files: {ex}"); } } private void DownloadGameCompletedCallback(object sender, AsyncCompletedEventArgs e) { try { // 先检查异步下载是否出错 if (e.Error != null) { Status = LauncherStatus.failed; MessageBox.Show($"Download error: {e.Error.Message}"); return; } string onlineVersion = ((Version)e.UserState).ToString(); ZipFile.ExtractToDirectory(gameZip, rootPath, true); File.Delete(gameZip); File.WriteAllText(versionFile, onlineVersion); VersionText.Text = onlineVersion; Status = LauncherStatus.ready; } catch (Exception ex) { Status = LauncherStatus.failed; MessageBox.Show($"Error extracting files: {ex}"); } }
额外优化建议
- 在
DownloadGameCompletedCallback中优先检查e.Error,提前处理下载阶段的错误,避免进入无效的解压流程。 - 可添加文件大小校验逻辑,对比下载后文件的大小与服务器返回的Content-Length,确保文件下载完整。
内容的提问来源于stack exchange,提问作者Rooster Rich
相关产品推荐
相关产品推荐

