【问题标题】:How to create and download a zip file in ASP.NET Core?如何在 ASP.NET Core 中创建和下载 zip 文件?
【发布时间】:2018-06-29 09:58:04
【问题描述】:

我们需要以 Zip 格式捆绑多个文件并下载它。您能否建议一种在 ASP.NET 核心中执行此操作而不使用任何第三方库的方法。

在 ASP.NET MVC 中,我们可以使用https://msdn.microsoft.com/en-us/library/system.io.packaging.aspx 来实现这一点。在 ASP.NET core 2.0 中是否可以?

【问题讨论】:

  • 本网站上已经有多个关于该主题的答案。搜索它并尝试一些建议。如果您遇到问题,请提供问题的minimal reproducible example

标签: asp.net-core-2.0 asp.net-core-1.0


【解决方案1】:

我认为这对你有很大帮助

protected FileStreamResult DownloadFolder(string path, string[] names, int count)
{
    FileStreamResult fileStreamResult;
    var tempPath = Path.Combine(Path.GetTempPath(), "temp.zip");
    if (names.Length == 1)
    {
        path = path.Remove(path.Length - 1);
        ZipFile.CreateFromDirectory(path, tempPath, CompressionLevel.Fastest, true);
        FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
        fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
        fileStreamResult.FileDownloadName = names[0] + ".zip";
    }
    else
    {
        string extension;
        string currentDirectory;
        ZipArchiveEntry zipEntry;
        ZipArchive archive;
        if (count == 0)
        {
            string directory = Path.GetDirectoryName(path);
            string rootFolder = Path.GetDirectoryName(directory);
            using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
            {
                for (var i = 0; i < names.Length; i++)
                {
                    currentDirectory = Path.Combine(rootFolder, names[i]);
                    foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
                    {
                        zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, names[i] + filePath.Substring(currentDirectory.Length), CompressionLevel.Fastest);
                    }
                }
            }
        }
        else
        {
            string lastSelected = names[names.Length - 1];
            string selectedExtension = Path.GetExtension(lastSelected);
            if (selectedExtension == "")
            {
                path = Path.GetDirectoryName(Path.GetDirectoryName(path));
                path = path.Replace("\\", "/") + "/";

            }
            using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
            {
                for (var i = 0; i < names.Length; i++)
                {
                    extension = Path.GetExtension(names[i]);
                    currentDirectory = Path.Combine(path, names[i]);
                    if (extension == "")
                    {
                        foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
                        {
                            zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, filePath.Substring(path.Length), CompressionLevel.Fastest);
                        }
                    }
                    else
                    {
                        zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + currentDirectory, names[i], CompressionLevel.Fastest);
                    }
                }
            }

        }
        FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
        fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
        fileStreamResult.FileDownloadName = "folders.zip";
    }
    if (File.Exists(tempPath))
    {
        File.Delete(tempPath);
    }
    return fileStreamResult;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 1970-01-01
  • 2011-02-21
  • 2017-05-13
相关资源
最近更新 更多