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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With