Narendra Choudhary
Narendra Choudhary

Reputation: 75

'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));
            }
        }
    }
}

Upvotes: 6

Views: 2987

Answers (2)

Mohammed Osman
Mohammed Osman

Reputation: 4246

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

Upvotes: 1

Hugo Delsing
Hugo Delsing

Reputation: 14173

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.

Upvotes: 9

Related Questions