Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 471
manicdrummer Avatar asked Apr 24 '26 04:04

manicdrummer


1 Answers

For some reason, OpenRead does not exist in net46 assembly. The quick workaround is to use

ZipArchive OpenRead(string filename)
{
    return new ZipArchive(File.OpenRead(filename), ZipArchiveMode.Read);
}

as answered at https://stackoverflow.com/a/44598092/75947

like image 66
Felix Avatar answered Apr 25 '26 19:04

Felix