代码之家  ›  专栏  ›  技术社区  ›  Shay Guy

为什么corert编译的程序不能处理zip文件?

  •  1
  • Shay Guy  · 技术社区  · 7 年前

    我编写了以下简单的程序来测试:

    using System;
    using System.IO;
    using System.IO.Compression;
    
    namespace HelloZip
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = Path.Combine(AppContext.BaseDirectory, "test.zip");
    
                using (ZipArchive z = ZipFile.Open(path, ZipArchiveMode.Create))
                {
                    var f = z.CreateEntry("hello.txt");
                    using (StreamWriter writer = new StreamWriter(f.Open()))
                    {
                        writer.Write("Hello World!");
                    }
                }
            }
        }
    }
    

    this page 我跑 dotnet new nuget 在hellozip项目目录中,在nuget.config中添加了推荐的包源,然后运行以下命令:

    dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
    dotnet publish -r win-x64 -c release -o out
    out\HelloZip.exe
    

    我得到以下错误:

    Unhandled Exception: System.IO.Compression.ZLibException: The underlying compression routine could not be loaded correctly. ---> System.DllNotFoundException: Unable to load DLL 'clrcompression.dll': The specified module could not be found.
       at HelloZip!<BaseAddress>+0x1199e7
       at HelloZip!<BaseAddress>+0x119904
       at Interop.zlib.DeflateInit2_(ZLibNative.ZStream&, ZLibNative.CompressionLevel, ZLibNative.CompressionMethod, Int32, Int32, ZLibNative.CompressionStrategy) + 0x48
       at System.IO.Compression.ZLibNative.ZLibStreamHandle.DeflateInit2_(ZLibNative.CompressionLevel, Int32, Int32, ZLibNative.CompressionStrategy) + 0x4f
       at System.IO.Compression.Deflater.DeflateInit(ZLibNative.CompressionLevel, Int32, Int32, ZLibNative.CompressionStrategy) + 0x59
    
       --- End of inner exception stack trace ---
       at System.IO.Compression.Deflater.DeflateInit(ZLibNative.CompressionLevel, Int32, Int32, ZLibNative.CompressionStrategy) + 0x1db
       at System.IO.Compression.Deflater..ctor(CompressionLevel, Int32) + 0x50
       at System.IO.Compression.DeflateStream.InitializeDeflater(Stream, Boolean, Int32, CompressionLevel) + 0x39
       at System.IO.Compression.ZipArchiveEntry.GetDataCompressor(Stream, Boolean, EventHandler) + 0x4b
       at System.IO.Compression.ZipArchiveEntry.OpenInWriteMode() + 0x65
       at HelloZip.Program.Main(String[]) + 0x75
       at HelloZip!<BaseAddress>+0x1716a2
    

    是我犯了个错误,还是aot编译器还没有正确地支持system.io.compression?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Michal Strehovský    7 年前

    您正在使用一个已知的未实现功能。解决方法是暂时手动复制编译的可执行文件旁边的clrcomplication.dll。请参见此处的详细信息: https://github.com/dotnet/corert/issues/5496