c# - 使用 SharpZipLib 压缩文件时出现 "Invalid folder"错误

标签 c# asp.net zip sharpziplib

我有一个函数,用于压缩用户 session 中的文件列表,然后将其流式传输到用户的浏览器以供下载:

public static void DownloadAllPhotos()
{
    HttpContext.Current.Response.AddHeader(
        "Content-Disposition", "attachment; filename=Photos.zip");
    HttpContext.Current.Response.ContentType = "application/zip";

    List<string> photos= new List<string>();

    if (HttpContext.Current.Session != null && 
        HttpContext.Current.Session["userPhotos"] != null)
    {
        photos = (List<string>)HttpContext.Current.Session["userPhotos"];
    }

    using (var zipStream = new 
        ZipOutputStream(HttpContext.Current.Response.OutputStream))
    {
        foreach (string photoUrl in photos)
        {
            byte[] fileBytes = File.ReadAllBytes(photoUrl);

            var fileEntry = new ZipEntry(
                Path.GetFileName(photoUrl))
            {
                Size = fileBytes.Length
            };

            zipStream.PutNextEntry(fileEntry);
            zipStream.Write(fileBytes, 0, fileBytes.Length);
        }

        zipStream.Flush();
        zipStream.Close();

        // reset session
        HttpContext.Current.Session["userPhotos"] = new List<string>();
    }
}

当用户在其 session 中具有照片网址并单击按钮来调用此函数时,文件将被压缩并在用户的浏览器中开始下载。

但是当我尝试打开压缩文件时,出现此错误:

Windows cannot open the folder.

The compressed folder "{Path to my file}" is invalid.

我是否做错了什么导致了这个错误?

最佳答案

查看 this exampleResponse.FlushZipEntry.CleanName 的位置并看看编写类似的内容是否可以解决问题。

关于c# - 使用 SharpZipLib 压缩文件时出现 "Invalid folder"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376519/

相关文章:

c# - 浏览 FindContours 方法找到的轮廓层次结构?

linux - 使用 xz 而不是 gz - 非常慢

c# - ASP.NET 如何获取 Active Directory 中的组列表

asp.net - 我想在 div 中使用 li 标签,但不推荐这样做,还有其他方法吗?

c# - 未翻译所需的数据注释

python - 访问 pandas to_csv 中的 zip 压缩选项

javascript - 如何知道 npm `unzip` 模块何时完成解压缩文件?

c# - 归档 zip 文件并将其保存到选择的位置,而不需要额外的文件路径?

c# - 如何在 C# 项目中使用 Scintilla .NET?

c# - 使用 Revit API 在模型中放置自定义体量族实例