Unzip archive in .Net CORE 1.0

2019-07-13 01:32发布

问题:

I'm currently working on an ASP.NET Core 1.0 project. I want to stay on the dnxcore50 and the dnx451 at the same time. Which brings me to the following issue now:

How can I unzip a zip archive from the filesystem to a directory?

"System.IO.FileSystem.Primitives": "4.0.0-beta-22816",
"System.IO.FileSystem": "4.0.0-beta-22816"

"frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.IO": ""
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.IO": "4.0.10-beta-*"
      }
    }
  }

With that configuration in the project.json I'm now able to get a GZipStream but I have no idea how to store that stream to a directory. Unfortunately the ZipFile object in System.IO.Compression.ZipFile.ExtractToDirectory() method doesn't exsists in the core framework :/.

UPDATE:

I now included it that way and removed it from the overall dependencies:

"frameworks": {
"dnx451": {
  "dependencies": {
    "System.IO.Compression.ZipFile": "4.0.1-beta-23516"
  }
},
"dnxcore50": {
  "dependencies": {
    "System.IO.Compression.ZipFile": "4.0.1-beta-23516"
  }
}

When I run the application using core it works as expected, but running it with the .net framework 4.6 I crashes with the following exeception during runtime:

n unhandled exception occurred while processing the request.

An unhandled exception occurred while processing the request.
FileNotFoundException: Could not load file or assembly 'System.IO.Compression.ZipFile, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

回答1:

The dependencies you require in your project.json are as follows.

"frameworks": {
    "net451": {
      "frameworkAssemblies": {
        "System.IO.Compression": "4.0.0.0",
        "System.IO.Compression.FileSystem":  "4.0.0.0"
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.IO.Compression.ZipFile": "4.0.1-beta-23516"
      }
    }
  },

The dependenices in the root of project.json are for any packages that work with all runtimes you are using.

The frameworkAssemblies in the .net runtime are the same references you would add in a normal csproj based project. You can find the assemblies here if you are not sure what to add. https://msdn.microsoft.com/en-us/library/mt472912(v=vs.110).aspx. You would use the dependencies section here for any net specific references.

When it comes to dnxcore50 the dependencies references come straight from nuget but you can use the intellisense to find these.

More information about project.json can be found here https://github.com/aspnet/Home/wiki/Project.json-file