Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

C# Compression.Zip missing?

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

  1. TheRobWatling

    TheRobWatling

    Joined:
    Feb 18, 2013
    Posts:
    20
    tigerleapgorge likes this.
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    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?
     
    tigerleapgorge likes this.
  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;
     
    tigerleapgorge likes this.
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    Which version of Unity are you using?
     
    tigerleapgorge likes this.
  5. RWatling

    RWatling

    Joined:
    Mar 21, 2018
    Posts:
    9
    2018.2.0f2

    AnneSchmidt, Yes added it as a namespace.
     
    tigerleapgorge likes this.
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    @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.
     
  7. RWatling

    RWatling

    Joined:
    Mar 21, 2018
    Posts:
    9
    Thanks so much for looking into this, greatly appreciate the rapid responses!
     
    tigerleapgorge likes this.
  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
     
    tigerleapgorge likes this.
  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:
    4,640
    @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.
     
  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!
     
    tigerleapgorge likes this.
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    What is the value of the "Scripting Runtime Version" option in the Player Settings?
     
    tigerleapgorge likes this.
  13. AcademyOfFetishes

    AcademyOfFetishes

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

     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    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:
    4,640
    Thanks, we will investigate this.
     
    AcademyOfFetishes likes this.
  18. KrabbyQ

    KrabbyQ

    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:
    4,640
    That is a bit odd, but might be possible.

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

    HEROTECH70

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

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    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:
    4,640
    It looks like out csc.rsp file has a reference to System.IO.Compression.FileSystem.dll, but ZipArchive is in System.IO.Compression.dll.
     
    Gregg-Patton and KBVDev like this.
  25. safu

    safu

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

    -r:System.IO.Compression.dll
    -r:System.IO.Compression.FileSystem.dll
     
    andymads and manugulus like this.
  26. Altaireon

    Altaireon

    Joined:
    Oct 23, 2018
    Posts:
    4
    This happened to me in Unity 2018.4.12f1 but then i restarted Unity and Visual Studio and on the next run it did some assembly compilation and the error went away.
     
  27. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    190
    My notes:

    In Unity 2019.1 there is no need to add csc.rsp. At least with Android and Editor everything works with .zip files.

    But with Unity 2019.3, we have to add csc.rsp file. Otherwise there are compilation errors and no Editor play is possible.
     
    tigerleapgorge likes this.
  28. Rodriguez-Claverias

    Rodriguez-Claverias

    Joined:
    Nov 29, 2017
    Posts:
    2
    Hi,

    I was looking for information about this bug and temporarily solved it in the editor. My problem is that when I run the code snippet on Android I receive the following error:

    FileNotFoundException: Could not load file or assembly 'System.IO.Compression.FileSystem, version=4.0.0.0, culture=neutral, ....' or one of its dependencies.

    Can you help me? What I can do?

    Thanks in advantage
     
  29. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,525
    @JoshPeterson I was having a strange issue with this. Two projects, both 2019.2.17, both with same API Compatibility Level set, both with exact same script file.

    In one, no errors, in the other, errors as described above. I don't get it.

    I tried @safu's solution above and that fixed the errors.
     
  30. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    I'm not sure what could cause this. There must be some difference between the projects that matters. Can you diff all of the files in the project?
     
  31. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,525
    I reduced the project down to just my script and an blank file in an editor folder. The error remains until the blank file is deleted or moved out of the editor folder.

    Screenshot 2020-02-21 at 15.26.23.png
     
  32. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    Can you submit a bug report for this issue then?
     
  33. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,525
    Sure. Will do.

    Case 1221833.
     
    Last edited: Mar 25, 2020
    bodisafa likes this.
  34. bodisafa

    bodisafa

    Joined:
    Feb 26, 2018
    Posts:
    1
    Thankful that this thread exists. Ran into the same issue and used the same fix.
     
  35. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    79
    What is the procedure for saving a build as a Zip if the scripting run time version is .NET 3.5 Equivalent with an APi Compatibility Level of .NET 2.0 Subset? I'm trying this in 2018.2.21f1 and it says ZipFile does not exist in the current context?

    An above link states
    .NET standard 2.0 profile
    If your Project uses the .NET Standard 2.0 API Compatibility Level, you don’t need to take any additional steps to use part of the .NET class library API. If part of the API seems to be missing, it might not be included with .NET Standard 2.0. The Project might need to use the .NET 4.x API Compatibility Level instead.

    Do I have to update to 4.0 then? Is there any way to automatically zip files every time I run a build with current settings?
     
  36. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    The naming here can be confusing. The API Compatibility Level of .NET 2.0 Subset, is not the same as .NET Standard 2.0. They are entirely different.

    I would recommend that you change the scripting run time version to .NET 4.x. In Unity 2019 and later, the .NET 3.5 equivalent scripting runtime version is no longer supported.
     
  37. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,001
    Catbert is the lead of Microsoft's naming stuff department. That's the only explanation that makes sense.
     
  38. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    79
    Thanks guys. I tried this and get multiple errors.

    Unloading broken assembly Assets/Editor Default Resources/UMotionEditor/Plugins/Application/UMotionApplication.dll, this assembly can cause crashes in the runtime an issue that was not present before adding the csc.rsp and
    The type `System.IO.Compression.ZipFile' has been forwarded to an assembly that is not referenced. Consider adding a reference to assembly `System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral... Also The `.' operator cannot be applied to operand of type `System.IO.Compression.ZipFile'

    Using csc with both lines:

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

    and Script

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEditor.Callbacks;
    5. using System.IO;
    6. using System.IO.Compression;
    7.  
    8.  
    9.  
    10. // place in an "Editor" folder in Assets folder
    11. public class BuildZipPostProcess : MonoBehaviour
    12. {
    13.     [PostProcessBuild(0)]
    14.     public static void OnPostProcessBuild(BuildTarget target, string buildPath)
    15.     {
    16.         if (!File.Exists(Path.GetDirectoryName(buildPath) + "/_config.json"))
    17.             FileUtil.CopyFileOrDirectory("./_appinfo.json", Path.GetDirectoryName(buildPath) + "/_appinfo.json");
    18.  
    19.  
    20.         string buildDir = new DirectoryInfo(buildPath).Parent.FullName;
    21.         string zipPath = new DirectoryInfo(buildPath).Parent.Parent.FullName + $@"\builds\Test.zip"; //Can the .zip be made a custom extension?
    22.  
    23.         ZipFile.CreateFromDirectory(buildDir, zipPath);
    24.     }
    25.  
    26. }
     
  39. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    What Api Compatibility Level option are you using?
     
  40. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    79
    .NET 4.x
     
  41. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    Thanks! I think the csc.rsp file should contain only:

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

    I don't think there is a need for -r:System.IO.Compression.dll, but I'm not certain.

    Also, I've curious about the error related to Assets/Editor Default Resources/UMotionEditor/Plugins/Application/UMotionApplication.dll. Do you know how that assembly was built?
     
  42. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    79
    I believe its an animation plug in. I had not seen the error until changing to .Net 4x When I use just -r:System.IO.Compression.FileSystem.dll the UMotion error goes away but I still see

    Assets/Editor/BuildZipPostProcess.cs(22,9): error CS1070: The type `System.IO.Compression.ZipFile' has been forwarded to an assembly that is not referenced. Consider adding a reference to assembly `System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    and

    Assets/Editor/BuildZipPostProcess.cs(22,17): error CS0023: The `.' operator cannot be applied to operand of type `System.IO.Compression.ZipFile'
     
  43. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    I'm not sure then. Is this a project you can send via a bug report? Maybe we should have a closer look at it.
     
  44. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    79
    Unfortunately I wont be able to send it. Thank you though.
     
  45. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    Can you reproduce this in a simpler project that you can send?
     
  46. mrekuc

    mrekuc

    Joined:
    Apr 17, 2009
    Posts:
    65
    I had the same issue. Even though I had
    Code (CSharp):
    1. using System.IO.Compression;
    i still got the error. So I changed my script from
    Code (CSharp):
    1. ZipFile.ExtractToDirectory(zipPath, extractPath);
    to
    Code (CSharp):
    1. System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
    And it works
     
  47. PassivePicasso

    PassivePicasso

    Joined:
    Sep 17, 2012
    Posts:
    88
    I'm not sure if I'm misinterpretting the issues coming up here, but I'm running into this issue I believe.

    I'm working on a Package that will be distributed hopefully via package manager, so I don't think I can use the csc.rsp solution otherwise I may pollute consumers setup.

    This issue comes up when I'm writing editor scripts, I'm using AssemblyDefinitions for my project.

    This appears to make the Editor folder a partially irrelevant concept, other than the unity tooling. The compile is directed by the AssemblyDefinitions if my understanding is correct. (Please verify?)

    I'm working in Unity 2018.4.16f1

    I can reproduce this easily, and kinda solve it, but its buggy.
    Make a new project.
    Add a folder to the project, and add an AssemblyDefinition.
    In the AssemblyDefinition include Editor only.

    Now create an editor script in that folder, attempt to use ZipFile or ZipArchive and they will not be found.

    This can be remedied by adding Windows 32bit and/or Windows 64bit to platforms, but for editor scripts this causes problems since now builds will attempt to include them and be unable to do so, requiring you to work around it by including a compiler directive.

    If the Windows 32bit and 64bit build targets include it automatically, why not the editor by default?
    Is there a fix for this which would be stable in a Package?
     
    Elmak likes this.
  48. PassivePicasso

    PassivePicasso

    Joined:
    Sep 17, 2012
    Posts:
    88
    So while I have this working In my 2018 version of the project, I need to have this working in Unity 2017.

    Its been stated that manually/automatically copying these files isn't a reliable approach, will a fix be back ported to unity 2017 LTS?
     
  49. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,640
    I do not expect this to be back ported to 2017 LTS, sorry!
     
  50. PassivePicasso

    PassivePicasso

    Joined:
    Sep 17, 2012
    Posts:
    88
    Is there a recommended approach for getting these libraries into a Unity 2017 project?
    I'm trying to get a package I've built to be automatically configured so all the user has to do is add the package to their assets folder in 2017 (since we don't get package manager)

    I'm currently looking at this by going with EditorApplication.applicationPath and then attempting to resolve the correct assembly to load from the mono folders, but I don't see an appropriate map I can utilize to accomplish this.
     
unityunity