Search Unity

C# Compression.Zip missing?

Discussion in 'Experimental Scripting Previews' started by TheRobWatling, Nov 1, 2018.

  1. TheRobWatling

    TheRobWatling

    Joined:
    Feb 18, 2013
    Posts:
    16
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    I don't see a System.IO.Compression.Zip class. It looks like there are classes named ZipFile and ZipArchive though. Are you referring to one of those?
     
  3. RWatling

    RWatling

    Joined:
    Mar 21, 2018
    Posts:
    9
    Hi, Thanks for the quick response.
    Yes those are the classes I'm after but cannot get access to them.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile?view=netframework-4.7.2
    https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view=netframework-4.7.2

    neither of these can be found when including the Name spaces:

    System.IO;
    System.IO.Compression;
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    Which version of Unity are you using?
     
  5. RWatling

    RWatling

    Joined:
    Mar 21, 2018
    Posts:
    9
    2018.2.0f2

    AnneSchmidt, Yes added it as a namespace.
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    @RWatling

    These types are in the System.IO.Compression.FileSystem.dll assembly, which is not passed as a reference by Unity to the C# compiler by default. You can create a file named mcs.rsp in the Assets/ directory of your Unity project to let the C# compiler know you want this additional reference.

    The mcs.rsp file should have the following contents:

    -r:System.IO.Compression.FileSystem.dll

    See the documentation here for more details: https://docs.unity3d.com/Manual/dotnetProfileAssemblies.html

    Note that in Unity 2018.3, this file should be renamed to csc.rsp, instead of mcs.rsp.
     
    bstockwell, galaskap, n8fern and 2 others like this.
  7. RWatling

    RWatling

    Joined:
    Mar 21, 2018
    Posts:
    9
    Thanks so much for looking into this, greatly appreciate the rapid responses!
     
  8. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    This is not doing anything for me (and also that documentation says it should be named mcs.rsp). The only difference I can think of is that I'm trying to add this reference to an Editor script and my Editor directory has its own asmdef. Do the rules change in this situation?

    In VS, I can add the reference and in VS the compile error goes away, but in unity, I still get the error:



    I'm using 2018.3
     
  9. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    OK I figured it out. I figure, all this information above used to be true, but is now obsolete. Here's how you really do it:

    1. Find the ddl (and I believe optionally the XML file) on your file system.
    2. Copy it into your Assets directory.

    That's it. No *rsp files, no "adding references," just these two steps.

    Where do you get the ddl? Well for me, it was at
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\System.IO.Compression.FileSystem.dll
    but it may be somewhere else for you.

    This brings with it an annoying error that seems to be harmless. https://forum.unity.com/threads/loading-assembly-failed-even-though-it-shouldnt-be-loaded.660184/
     
    Last edited: Apr 12, 2019
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    @AcademyOfFetishes

    Although this does work, it is not the recommended solution. This solution can be fragile in the future, as the System.IO.Compression.FileSystem.dll used should be the one that Unity ships with its class library implementation. The one copied from the .NET Framework 4.7.1 may or may not work with a future version of Unity.

    I think the rules should be the same with or without an asmdef file. There is one difference that might matter here. If you are using the .NET 4.x Equivalent scripting runtime (the default with Unity 2018.3), you will need to name the file csc.rsp. The name mcs.rsp is only used for the old, .NET 3.5 scripting runtime. Unfortunately the documentation for this was out of date (I've since submitted a documentation change to correct this).

    This does make sense, because the generation of the .csproj file for Visual Studio is a one-way operation, from Unity to Visual Studio. No changes made in Visual Studio to the project references will be used by Unity. However, once Unity has a proper csc.rsp file in the project, the references should be correct in the .csproj file Unity generates.
     
    AcademyOfFetishes likes this.
  11. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    OK, good to know, but your steps still didn't work for me. That csc.rsp file has no effect on my project's ability to compile. I'll try to provide more info:

    I've deleted the System.IO.Compression.FileSystem dll and xml from my assets directory.

    I created Assets/csc.rsp with vim and inside I put this single line:
    -r:System.IO.Compression.FileSystem.dll
    . Unity doesn't seem to recognize this file as anything special, but I'm not sure if that's normal. Here's how its inspector looks:



    Here's that file in my project view:



    I also get this error, preventing me from compiling:



    Here's BuildPostprocessor file:

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.IO.Compression;
    4. using UnityEditor;
    5. using UnityEditor.Callbacks;
    6. using UnityEngine;
    7.  
    8. public class BuildPostprocessor
    9. {
    10.     [PostProcessBuildAttribute]
    11.     public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    12.     {
    13.         GameObject versionObj = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Plugins/GoogleAnalyticsV4/GAv4.prefab", typeof(GameObject));
    14.         GoogleAnalyticsV4 ga = versionObj.GetComponent<GoogleAnalyticsV4>();
    15.         string versionString = ga.bundleVersion;
    16.  
    17.         string buildDir = new DirectoryInfo(pathToBuiltProject).Parent.FullName;
    18.         string zipPath = new DirectoryInfo(pathToBuiltProject).Parent.Parent.FullName + $@"\builds\MonsterGirlFarmer-{versionString}-{GitSha.GetGitSha()}.zip";
    19.  
    20.         ZipFile.CreateFromDirectory(buildDir, zipPath);
    21.     }
    22.  
    23. }
    In VS, it also doesn't know how to compile ZipFile. This code compiles with the dll dragged in.

    Here's my unity version:



    Am I doing something wrong? I think I'm following your instructions. Thanks for the help!
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    What is the value of the "Scripting Runtime Version" option in the Player Settings?
     
  13. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    It looks like this:

     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    Wow, you should be using the Roslyn C# compiler then, and csc.rsp should be used. Can you submit this project with a bug report? We should investigate this more - something looks to be wrong.
     
    AcademyOfFetishes likes this.
  15. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    I submitted a bug report but it didn't give me any information afterward to refer you to it. If you want to find it, maybe search by my name or the title that mentioned csc
     
  16. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    JoshPeterson likes this.
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    Thanks, we will investigate this.
     
    AcademyOfFetishes likes this.
  18. SsuYuen

    SsuYuen

    Joined:
    Dec 19, 2016
    Posts:
    1
    JoshPeterson likes this.
  19. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    Hm... I saved a git stash of the issue, and when I popped it, the error didn't show up. What I think is the difference between then and now is that since I reported the bug, I deleted the Library directory. Maybe the library directory somehow had a problem in it that prevented the csc.rsp file from being read, and when I deleted it, now it can be read?
     
  20. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    That is a bit odd, but might be possible.

    @SsuYuen: Have you encountered something similar?
     
  21. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    24
    Adding the csc.rsp added the ZipFile class but no ZipArchive
     
  22. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    Can you provide a few more details? What is the content of the csc.rsp file? Also, what version of Unity are you using, and what is the "Scripting Runtime Version" value in the player settings.
     
  23. jmcgee412

    jmcgee412

    Joined:
    Oct 22, 2013
    Posts:
    1
    Has anyone been able to get the ZipArchive to be found? I am having the same issue. The csc.rsp file helped to find ZipFile but not ZipArchive....

    ZipArchive was not being found in VB until I added a reference to the Assembly for System.IO.Compression. After that, VB found ZipArchive but it is still saying it cannot be found in Unity

    I am using Unity Version 2018.3.6f1

    Here is the Unity error...

    upload_2019-5-15_15-21-48.png


    Here is my csc.rsp file...

    upload_2019-5-15_15-21-5.png


    Here are my Scripting Runtime Version setting...

    upload_2019-5-15_15-20-36.png
     
  24. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    3,965
    It looks like out csc.rsp file has a reference to System.IO.Compression.FileSystem.dll, but ZipArchive is in System.IO.Compression.dll.
     
    KBVDev likes this.
  25. safu

    safu

    Joined:
    Mar 23, 2019
    Posts:
    1
    I writed below lines to csc.rsp, and the errors are dissapeared.

    -r:System.IO.Compression.dll
    -r:System.IO.Compression.FileSystem.dll