''ZipArchiveEntry' does not contain the definition of 'ExtractToFile'
Using ASP.NET Framework 4.7.1, I am trying to compress a file but Visual Studio is showing that ZipArchiveEntry does not contain the definition of ExtractToFile.
This is my code:
[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));
}
}
}
}
Solution 1:[1]
Although most of the ZipArchive functionality can be added by including a reference to System.IO.Compression, to use the ExtractToFile option you also need a reference to System.IO.Compression.FileSystem.
It's not required to add it as a Using statement, just the reference is enough.
Solution 2:[2]
The extension methods CreateEntryFromFile(), ExtractToDirectory() and ExtractToFile() can be accessed after installing the package System.IO.Compression.ZipFile by using the following command:
Install-Package System.IO.Compression.ZipFile -Version 4.3.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hugo Delsing |
| Solution 2 | Mohammed Osman |
