Invalid il code System.IO.Compression.ZipFile.Open

2019-05-29 03:29发布

问题:

I 'am implementing an android app using Xamarin and MVVMCross. Inside my PCL I have a ZipUtility class and the following method:

public NoDataResponse UnCompressZipFile(string path, string filename)
   {
       NoDataResponse response = new NoDataResponse();
       StringBuilder sb = new StringBuilder();
       sb.AppendLine(string.Format("\r\nUnzipping '{0}' in '{1}' folder...", filename, path));

       try
       {       


           using (ZipArchive archive = ZipFile.Open(filename, ZipArchiveMode.Read))
           {
               foreach (System.IO.Compression.ZipArchiveEntry entry in archive.Entries)
               {
                   if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                   {
                       System.IO.Compression.ZipFile.ExtractToDirectory(filename, path);
                       break;
                   }
               }
           }


           response.IsCallSuccessful = true;
           sb.AppendLine(string.Format("\r\nFinished Unzipping file {0}.\r\n", filename));

           response.BusinessEntityMessage = sb.ToString();
       }

       catch (Exception ex)
       {
           response.IsCallSuccessful = false;
           response.BusinessEntityMessage = "\r\nUNZIPPING ERROR: " + ex.Message + "\r\n";
       }

       return response;
   }

The ZipFile Class inside my method is part of the System.IO.Compression (System.IO.Compression.FileSystem namespace).

When I execute the method I get the following error:

System.InvalidProgramException: Invalid IL code in System.IO.Compression.ZipFile:Open (string,System.IO.Compression.ZipArchiveMode): method body is empty. at HHT.Core.Globals.ZipUtility.UnCompressZipFile (System.String path, System.String filename) [0x00033] in c:\Sandbox\HHT-ANDROID\HHTApp\HHT.Core\Globals\ZipUtility.cs:32

I'm not sure what is missing because the same code was working perfectly before inside the PCL.

I have also tried to run the same class using dependency injection (N=31 slodge) from my DROIDUI project and still get the same error.

I would appreciate any comments or ideas on this.

回答1:

It sounds like you're deploying a reference assembly to your Android device (a reference assembly is only for the compilation and thus doesn't contain IL).

Which version of System.IO.Compression are you using? I assume you're using the Microsoft.Bcl.Compression NuGet package?

Our NuGet package doesn't provide an implementation for Android. In fact we only ship an implementation for Windows Phone -- for all other platforms we use the one that ships with the platform.

I'm not sure whether Xamarin supports the System.IO.Compression.dll (which includes ZipArchive). The reason it may have worked previously is because you retargeted the PCL after installing the NuGet package.



回答2:

Based on your findings so far, I bet you'll be able to fix the error by copying the Xamarin.Android System.IO.Compression.dll from the Mac installer into:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\

This assembly is currently (accidentally) missing from the Xamarin.Android Windows installer. A bug has been filed about this, so the assembly should be present by default in a future release.

If this doesn't work, be sure to report back that result so we can investigate further!