c# - c# 压缩多个文件并下载 - 无法在 Windows 资源管理器中打开文件
问题描述
我使用 SharpZipLib 压缩多个文件并将它们下载到我的 asp.net c# web 应用程序中。下载后我无法打开 zip 存档。Windows 错误消息是:
Windows 无法打开该文件夹。压缩(zipped)文件夹“filename.zip”无效。
这是我的代码
if (Session["lstFilesToZip"] != null)
{
List<string> filesToZip = (List<string>)Session["lstFilesToZip"];
if (filesToZip.Count > 0)
{
Response.AddHeader("Content-Disposition", "attachment; filename=fileName.zip");
Response.ContentType = "application/zip";
using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
foreach (string filePath in filesToZip)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
var fileEntry = new ZipEntry(Path.GetFileName(filePath))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
zipStream.Flush();
zipStream.Close();
}
}
}
我已经尝试添加
zipStream.Dispose();
zipStream.Finish();
没有成功。
也许有人可以帮忙。我找不到问题。
解决方案
推荐阅读
- informatica - 如何在 Informatica 中基于 Date Column 实现键范围分区
- javascript - 如何取消选中选项选择上的复选框,然后从数据库中加载相应的数据并保留复选框和选项选择状态
- tensorflow - 我安装了 tensforflow 并使用了该命令,但它显示以下错误
- react-native - 基于ajax调用在底部导航器中动态显示堆栈
- gitlab - 如何从 .net 项目文件中获取 gitlab-ci.yml 文件中的项目版本?
- android - 无法在片段中显示来自 Firestore 的回收视图项目
- php - Laravel:上传 64base 图像而不是普通图像
- cors - 如何在 Keycloak 中禁用 CORS
- python - 用上一个/下一个值 +- 100 在列中填充 Na
- python - 您能帮助理解 OR 运算符在此代码中的工作原理吗?