System.MissingMethodException when trying to read ZipFile from ZipArchive C#

Viewed 6182

I have a C# .NET (v4.6.2) WinForms app where I'm accessing a file that may/may not be a .zip archive that was created using "System.IO.Compression;". I have both "System.IO.Compression" and System.IO.Compress.FileSystem" references in the project and "using System.IO.Compression;" at the top, which was installed using the NuGet package Installer.

Below is the code for attempting to open the file as a .zip archive:

      try
        {
            string extractPath = Path.GetTempFileName();
            string strGameVersion = "";
            string strProjectType = "";

            using (ZipArchive archive = ZipFile.OpenRead(OpenFilePath))
            {
                FileStream fs = new FileStream(extractPath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.FullName.Contains("ProjectData.txt"))
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
                        strGameVersion = sr.ReadLine();
                        strProjectType = sr.ReadLine();
                    }
                    File.Delete(extractPath);
                }
                sr.Close();
                fs.Close();
                archive.Dispose();
            }
    }
    catch(System.IO.FileFormatException flex1)
    {
        MessageBox.Show(flex1.ToString(), "oops.", MessageBoxButtons.OK,  MessageBox.Icon.Error);
    }

The error message is "System.MissingMethodException: Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'." So what am I doing wrong or not doing at all?

3 Answers

I had to switch over to using System.IO.Compression from nuget.org to get this to work. Plus I had to make the changes that Felix suggested above. That is replace:

ZipFile.OpenRead(file))

with

new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read)

Related