首页 > 解决方案 > “ZipArchiveEntry”不包含“ExtractToFile”的定义

问题描述

使用 ASP.NET Framework 4.7.1,我试图压缩一个文件,但 Visual Studio 显示ZipArchiveEntry不包含ExtractToFile.

这是我的代码:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file == null) 
        return View();

    string path = Server.MapPath("~/App_Data/Uploads");

    using(ZipArchive archive = new ZipArchive(file.InputStream))
    {
        foreach(ZipArchiveEntry entry in archive.Entries)
        {
            if(!string.IsNullOrEmpty(Path.GetExtension(entry.FullName)))
            {
                entry.ExtractToFile(Path.Combine(path, entry.FullName));
            }
            else
            {
                Directory.CreateDirectory(Path.Combine(path, entry.FullName));
            }
        }
    }
}

标签: c#asp.netasp.net-mvc

解决方案


ZipArchive尽管可以通过引用来添加大部分功能System.IO.Compression,但要使用该ExtractToFile选项,您还需要引用System.IO.Compression.FileSystem.

不需要将其添加为Using语句,只需引用即可。


推荐阅读