c# - ZipArchive 在实时服务器上提供无效文件

标签 c# asp.net iis

我在处理程序中使用 ZipArchive 来为使用内存流和 Web 处理程序的用户提供服务。在我将应用程序上传到实时站点之前,这在本地一直有效。

这是我的代码。

using (ZipArchive newArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
    newArchive.CreateEntryFromFile(fileName, Path.GetFileName(fileName));
    if (File.Exists(acRefFile))
    {
        newArchive.CreateEntryFromFile(acRefFile,
            newACRefName + Path.GetExtension(acRefFile));
    }
    else
    {
        SystemLogManager sysLogMgr = new SystemLogManager();
        sysLogMgr.AddErrorMessage(acRefFile, "File not found");
    }
    if (File.Exists(exRefFile))
    {
        newArchive.CreateEntryFromFile(exRefFile,
            newExRefName + Path.GetExtension(exRefFile));
    }
    else
    {
        SystemLogManager sysLogMgr = new SystemLogManager();
        sysLogMgr.AddErrorMessage(exRefFile, "File Not Found");
    }
    if (File.Exists(exRef2File))
    {
        newArchive.CreateEntryFromFile(exRef2File,
            newExRef2Name + Path.GetExtension(exRef2File));
    }
}
memoryStream.Position = 0;
byte[] bytes = memoryStream.GetBuffer();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.ContentType = "application/zip";
context.Response.AddHeader("content-disposition",
    string.Format("attachment; filename =app_{0}_{1}.zip", appForm.Cand_sno,
        appForm.App_year));
context.Response.BinaryWrite(bytes.ToArray());
context.Response.Flush();

下图显示了下载的 zip 文件和生成的错误。 Zip error

那么代码中是否有任何错误或我可以在服务器端尝试的东西?

更新 1: 根据收到的评论,我尝试将 zip 文件直接添加到服务器上。出现与 zip 中“损坏”相同的问题。

更新 2: 进一步调查我现在发现 zip 文件在使用 7zip 而不是标准的 Windows 提取时打开。当右键单击提取所有消息时,说明 zip 为空。

谢谢

最佳答案

所以这个问题的解决方法只是将 byte[] bytes = MemoryStream.GetBuffer(); 更改为 byte[] bytes = MemoryStream.ToArray(); 这只是获取已使用的字节,而不是缓冲区添加的额外字节。

关于c# - ZipArchive 在实时服务器上提供无效文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734387/

相关文章:

apache - 从 apache 到 IIS 的反向代理总是失败并显示 404

c# - 如何在 C# 代码中访问 C++ 类?

c# - Visual Studio 类图未显示关系

C# SQL 聚合 ExecuteScalar 返回查询

asp.net - 如何以零停机时间部署 ASP.NET 应用程序

.htaccess - 如何将此 .htaccess 转换为 IIS web.config?

c# - LINQ,左连接,仅获取连接表中的空位置

asp.net - 无法从程序集中加载 BuildTasks.Csc 任务?

asp.net - 相当于 Sitecore 中的 Razor 部分助手

asp.net - 如何使用自定义端口在 IIS 中查找网站