【问题标题】:Unzipping throws an "The underlying compression routine could not be loaded correctly"解压缩会引发“无法正确加载底层压缩例程”
【发布时间】:2017-05-02 07:16:32
【问题描述】:

尝试使用以下代码在 Win10 移动版 (UWP) 上解压缩文件

using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
{
  ZipArchiveEntry entry = archive.Entries.First();
  using (Stream reader = entry.Open())
  {

在 entry.Open() 处抛出以下错误。 “无法正确加载底层压缩程序。”

内部异常

“无法加载 DLL 'clrcompression.dll':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)”

重要提示:

  • entry.Open() 在 win10 桌面上运行良好,但在具有相同项目 (UWP) 的 Win10 移动设备上无法正常运行
  • 使用Win10移动版10.0.14393.576。 (Lumia 650)
  • 项目的UWP目标版本为10.0 BUILD 14393
  • 桌面版和移动版使用相同的文件。
  • 文件是在后端创建的,很简单

using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create,true))

  • 我正在使用 Xamarin 表单(我认为这无关紧要)

问题: 有人遇到过同样的例外吗?它的根本原因是什么? 有哪些可用的解决方法? (我需要跨平台压缩文件)

更新:我按照要求创建了简单的测试项目(感谢您对其进行调查)。

解压缩在测试项目中有效,但它正在加载不同的模块。 Debug/Modules 窗口指出抛出异常的项目使用 System.IO.Compression.dll 4.06.24705.1,工作项目使用 1.00.24301.1。

我上传了两个 project.lock.json 文件以供比较https://1drv.ms/f/s!AqROiejT4oI3lL1q1tu3iJcfA2tKyg

【问题讨论】:

  • 对我来说,就像您使用的 zip 库有一些依赖项存在于 Windows 10 的桌面版本中,而这些依赖项不存在于移动版本中
  • 是的,我也这么认为,但是如果不是 ZipArchive api,诚实的 .NETer 应该使用什么? (这是 .NET 的一部分)
  • 我一直使用 DotNetZip 库:github.com/haf/DotNetZip.Semverd
  • 查看 GitHub 和 nuget 看起来它没有任何依赖项阻止它跨平台。我会试一试。谢谢
  • 我无法重现您的问题。我正在使用与您的操作系统版本相同的 Lumia 640 进行测试。您是否尝试过使用 UWP 项目而不是 Xamarin Forms?如果您仍有问题,请分享minimal reproducible example,以便我们重现您的问题?

标签: c# .net zip compression uwp


【解决方案1】:

我找到了根本原因。正如 Jay Zuo 所说,问题在于 project.lock.json 以某种方式引用了“System.IO.Compression 4.3.0”。

我没有在任何 project.json 文件中直接引用它。我通过 .NETSTANDARD1.1 项目间接引用它,并引用“NETStandard.Library”:“1.6.1”。

通过使用 nuget.org,我使用旧的 System.IO.Compression 4.1.1 搜索了“NETStandard.Library”版本,即“NETStandard.Library”1.6.0。

使用“NETStandard.Library”1.6.0。在 .NETSTANDARD 1.1 项目中修复了解压缩错误。

我上传了一个示例解决方案来重现错误。 https://1drv.ms/f/s!AqROiejT4oI3lMMc_jWogCQy36awrA

【讨论】:

  • 我遇到了同样的问题,但我在引用列表中看不到任何对 Net 标准库的引用(也许另一个引用像你说的那样间接引用它)。但是哪一个?我可以使用 nuget 包管理器手动安装旧版本吗?我试过了,结果还是一样,如何让它加载旧版本?
  • 您是否在所有项目的所有 lock.json 文件中都搜索过“System.IO.Compression”?如果您同时引用了两者,我不知道它将在运行时使用哪个 dll,但我认为它在我的项目中失败了。
【解决方案2】:

对我来说,解决方法是将其添加到 .csproj 文件中的第一个 PropertyGroup

<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>

Visual Studio 通常会为 .NET Standard 项目隐式引入 1.6.1,该项目具有有问题的 System.IO.Compression 4.3.0。该指令告诉它改为下拉 1.6.0,这取决于 System.IO.Compression 4.1.1(适用于 Windows Phone)。

只要确保您没有另一个 NuGet 引用,它正在拉入 4.3.0。 System.IO.Compression.dll 的正确文件版本是 1.0.24301.1,损坏的是 4.6.xxxx。

【讨论】:

    【解决方案3】:

    根据你上传的“project.lock.json”,我认为这里的问题可能与System.IO.Compression 4.3.0 package有关。

    在异常抛出项目中,您可以发现它使用“System.IO.Compression 4.3.0”,如下所示:

    "runtime.native.System.IO.Compression/4.3.0": {
      "type": "package",
      "dependencies": {
        "Microsoft.NETCore.Platforms": "1.1.0",
        "Microsoft.NETCore.Targets": "1.1.0"
      },
      "compile": {
        "lib/netstandard1.0/_._": {}
      },
      "runtime": {
        "lib/netstandard1.0/_._": {}
      }
    },
    ...
    "System.IO.Compression/4.3.0": {
      "type": "package",
      "dependencies": {
        "System.Buffers": "4.3.0",
        "System.Collections": "4.3.0",
        "System.Diagnostics.Debug": "4.3.0",
        "System.IO": "4.3.0",
        "System.Resources.ResourceManager": "4.3.0",
        "System.Runtime": "4.3.0",
        "System.Runtime.Extensions": "4.3.0",
        "System.Runtime.Handles": "4.3.0",
        "System.Runtime.InteropServices": "4.3.0",
        "System.Text.Encoding": "4.3.0",
        "System.Threading": "4.3.0",
        "System.Threading.Tasks": "4.3.0",
        "runtime.native.System.IO.Compression": "4.3.0"
      },
      ...
    

    在工作项目中,它使用“System.IO.Compression 4.1.1”:

    "runtime.native.System.IO.Compression/4.1.0": {
      "type": "package",
      "dependencies": {
        "Microsoft.NETCore.Platforms": "1.0.1",
        "Microsoft.NETCore.Targets": "1.0.1"
      },
      "compile": {
        "lib/netstandard1.0/_._": {}
      },
      "runtime": {
        "lib/netstandard1.0/_._": {}
      }
    },
    ...
    "System.IO.Compression/4.1.1": {
      "type": "package",
      "dependencies": {
        "System.Collections": "4.0.11",
        "System.Diagnostics.Debug": "4.0.11",
        "System.IO": "4.1.0",
        "System.Resources.ResourceManager": "4.0.1",
        "System.Runtime": "4.1.0",
        "System.Runtime.Extensions": "4.1.0",
        "System.Runtime.Handles": "4.0.1",
        "System.Runtime.InteropServices": "4.1.0",
        "System.Text.Encoding": "4.0.11",
        "System.Threading": "4.0.11",
        "System.Threading.Tasks": "4.0.11",
        "runtime.native.System.IO.Compression": "4.1.0"
      },
      ...
    

    System.IO.Compression 4.3.0 package 似乎暂时不能在 UWP 项目中使用。目前最新稳定版“Microsoft.NETCore.UniversalWindowsPlatform”包使用“System.IO.Compression 4.1.1”包。您应该能够通过在其中添加System.IO.Compression 4.3.0 package 来重现工作项目的问题,如下所示:

    {
      "dependencies": {
        "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2",
        "System.IO.Compression": "4.3.0"
      },
      "frameworks": {
        "uap10.0": { }
      },
      "runtimes": {
        "win10-arm": { },
        "win10-arm-aot": { },
        "win10-x86": { },
        "win10-x86-aot": { },
        "win10-x64": { },
        "win10-x64-aot": { }
      }
    }
    

    所以要解决这个问题,我建议您检查项目的参考并确保没有“System.IO.Compression 4.3.0”包。如果确实需要使用“System.IO.Compression”包,可以尝试降级到“4.1.1”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多