Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

C# Compression.Zip missing?

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

  1. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    I don't have a good solution for this in 2017, sorry!
     
  2. Whitebrim

    Whitebrim

    Joined:
    Jul 11, 2017
    Posts:
    17
    C# Compression.Zip missing?
    Helped me in 2017.4
    "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.IO.Compression.FileSystem.dll"
     
  3. unleech

    unleech

    Joined:
    Jul 27, 2013
    Posts:
    17
    does this work in mac?

    mcs.rsp doesnt work in mac, but i copied that dll and it worked!
     
    Last edited: Jun 12, 2020
  4. PassivePicasso

    PassivePicasso

    Joined:
    Sep 17, 2012
    Posts:
    100
    I ended up using EditorApplicaiton.applicationPath and then navigating down to the gac folder to retrieve the assembly.
    I doubt this is the best solution, there are multiple library versions under this folder and I'm betting using Compiler directives to pick one based upon the current scripting back end is the better way to go.
    But using the GAC folder seemed to work fine.

    I ran into a bunch of other problems unrelated to this, so I donno if I would run into other issues using this solution.
     
  5. waydigitalsolutions

    waydigitalsolutions

    Joined:
    Dec 2, 2016
    Posts:
    7
    I ran into the same probem.
    System.IO.Compression.ZipFile can not be used after importing a plugin from the Unity Asset Store!

    This is very easy to replicate.
    Create new project and create a simple script that uses ZipFile functions.

    Code (CSharp):
    1. using System.IO.Compression;
    2. using UnityEngine;
    3.  
    4. public class asdf : MonoBehaviour
    5. {
    6.     [SerializeField] private string origin;
    7.     [SerializeField] private string destination;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         ZipFile.ExtractToDirectory(origin, destination);
    13.     }
    14. }
    In a fresh project without any plugins this should and will work!
    Now import any plugin. I tested this with the official 3D Game Kit Asset from Unity and some other plugins smaller asset store plugins (Online Maps v3, a random QR Code Reader asset and TriLib).

    After importing any one of them you will get an error.
    Assets\[YOUR_SCRIPT].cs(14,9): error CS0103: The name 'ZipFile' does not exist in the current context

    So. How can you fix this?

    Create a csc.rsp file with your favourite text editor and write the line:
    -r:System.IO.Compression.FileSystem.dll

    BUT, do not put the file in a random place in your project. I think it has to be directly in your Assets folder, otherwise the fix does not work!

    @JoshPeterson
    The only plugin that does not break this after importing was the Vectrosity plugin. It just gives you .unitypackage files you have to import manually after downloading.
    Maybe this helps.

    Cheers,
    Michael
     
  6. costingrigore6

    costingrigore6

    Joined:
    Feb 1, 2020
    Posts:
    1
    I know this has been solved a long time ago, but it still happens even in 2020, thank you again for the help.
     
  7. Whitebrim

    Whitebrim

    Joined:
    Jul 11, 2017
    Posts:
    17
    Create csc.rsp file and drop it into the Assets/ folder.

    Write inside:

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

    Kalikovision7

    Joined:
    Apr 7, 2020
    Posts:
    1
    I've encountered this issue a few times and each time it seems to be fixed by something else.

    First time I encountered it, I was misusing editor scripts in a sense. I had editor scripts not bundled into an assembly so I believe it was trying to pull the wrong references? Not entirely sure. But I added an assembly to wrap around my editor scripts specifically and that appeared to fix it for me. That might have been a fluke though and not the actual underlying issue. I only know this because I would remove that file from my project (deleted entirely) and it would work (and eventually I tested it with a barebone file as well I believe.

    This time I just had to restart unity and a different error popped up for me after being unable to find "ZipFile". I fixed that other error and then never had the zipFile error show up (this was a build that worked previously and no longer did on my laptop. So I know I didn't change anything else). My suspicion might be on me having done a 'rebuild all' from VS? Maybe something in Unity didn't have a chance to set up? No idea. But restarting Unity fixed it for me this past time. So I'm thankful at least for not having spent hours this time looking for it.
     
  9. joseGuate97

    joseGuate97

    Joined:
    Mar 1, 2018
    Posts:
    57
    So, there is no conclusion about what to do in... 2021, essentially, is there?
     
  10. Whitebrim

    Whitebrim

    Joined:
    Jul 11, 2017
    Posts:
    17
  11. azmi_unity

    azmi_unity

    Joined:
    Dec 13, 2020
    Posts:
    62
    Both these lines if you intend to use ZipArchive, etc..
     
  12. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I went through the same struggle and although the documentation mentions how to add the assembly reference to the rsp file, I still think this should be handled through editor UI. I'd like a dropdown to add references just like in Visual Studio or Rider. It doesn't have to be any fancy magic, just the list of available dlls that already ship with the Unity .Net distribution.
     
  13. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    One quick solution is to take your script out from the Editor folder and wrap it with
    #if UNITY_EDITOR
     
    lucbloom likes this.
  14. ActiveSim

    ActiveSim

    Joined:
    May 10, 2019
    Posts:
    59
    But that didn't work for me. The error is "Loading assembly failed". I added different dlls to my project. All of them are working directly (without the csc.rsp) but with the System.IO.Compression.dll and System.IO.Compression.FileSystem.dll it is different (with or without the csc file). I'm working with
    • Unity 2019.4.16f1
    • I use an assembly file for my scripts
    • The API compatibility level is .NET 4.x
     
    Last edited: Jun 8, 2021
  15. Whitebrim

    Whitebrim

    Joined:
    Jul 11, 2017
    Posts:
    17
    This issue have been fixed in latest Unity update:
    https://issuetracker.unity3d.com/is...m-dot-io-dot-compression-in-the-editor-folder
     
  16. ActiveSim

    ActiveSim

    Joined:
    May 10, 2019
    Posts:
    59
  17. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Thank you!

    Here's a asynchronous compression and extraction class if anyone's interested, use it inside of a calling IEnumerator method:

    Code (CSharp):
    1. using System.IO.Compression;
    2. using System.IO;
    3. using UnityEngine;
    4. using System.Threading.Tasks;
    5.  
    6.  
    7. namespace MG
    8. {
    9.     public class Zip
    10.     {
    11.  
    12.  
    13.         public static async Task<bool> CompressAsync(string folderPath, string fileDestination, bool deleteFolderPath = false)
    14.         {
    15.             if (Directory.Exists(folderPath) == false)
    16.             {
    17.                 Debug.LogError("No folderPath: " + folderPath);
    18.                 return true;
    19.             }
    20.  
    21.             await Task.Run(() => ZipFile.CreateFromDirectory(folderPath, fileDestination));
    22.  
    23.             if (deleteFolderPath)
    24.             {
    25.                 await Task.Run(() => Directory.Delete(folderPath));
    26.             }
    27.  
    28.             return true;
    29.         }
    30.  
    31.  
    32.         public static async Task<bool> ExtractAsync(string filePath, string folderDestination, bool deleteCompressionFile = false)
    33.         {
    34.             if (Directory.Exists(folderDestination) == true)
    35.             {
    36.                 Debug.LogError("Folder destination exists, can't extract: " + folderDestination);
    37.                 Debug.LogError("Please delete this first!");
    38.                 return true;
    39.             }
    40.  
    41.             string fileDirectory = Path.GetDirectoryName(filePath);
    42.  
    43.             if (Directory.Exists(fileDirectory) == false)
    44.                 Directory.CreateDirectory(fileDirectory);
    45.  
    46.  
    47.  
    48.             await Task.Run(() => ZipFile.ExtractToDirectory(filePath, folderDestination));
    49.  
    50.             if (deleteCompressionFile)
    51.             {
    52.                 await Task.Run(() => File.Delete(filePath));
    53.             }
    54.  
    55.             return true;
    56.         }
    57.  
    58.  
    59.     }
    60.  
    61. }
     
  18. OnatKorucu

    OnatKorucu

    Joined:
    Apr 18, 2018
    Posts:
    8
    I will try to give a little insight into why this is happening.

    What is happening?
    See what Rider offers you as a solution when it can not see the package:

    upload_2022-1-20_23-38-18.png

    If you reference System.IO.Compression.FileSystem, and open Solution window, you will see FileSystem being added.

    upload_2022-1-20_23-39-52.png

    But when you open Unity and come back to Rider, you will see that disappearing.

    What causes it?
    https://docs.unity3d.com/Manual/dotnetProfileAssemblies.html
    "If a Unity Project needs access to a part of the .NET class library API that is not compiled by default, the Project can inform the C# compiler in Unity. The behavior depends which .NET profile the Project uses."

    "Use a csc.rsp file to reference any other class library assemblies. You can add this file to the Assets directory of a Unity Project, and use it to pass additional command line arguments to the C# compiler. For example, if your Project uses the HttpClient class, which is defined in the System.Net.Http.dll assembly, the C# compiler might produce this initial error message:
    The type `HttpClient` is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

    To resolve this error, add the following csc.rsp file to the Project:

    -r:System.Net.Http.dll

    You should reference class library assemblies as described in the example above. You must place the assembly files in your Project’s Assets folder, or a subfolder of it."

    And what is csc.rsp?
    https://docs.microsoft.com/en-us/do...rence/compiler-options/miscellaneous#noconfig
    says:
    "The csc.rsp file references all the assemblies shipped with .NET Framework. The actual references that the Visual Studio .NET development environment includes depend on the project type. You can modify the csc.rsp file and specify additional compiler options that should be included in every compilation. If you don't want the compiler to look for and use the settings in the csc.rsp file, specify NoConfig. This compiler option is unavailable in Visual Studio and cannot be changed programmatically."

    What is in Roslyn csc.rsp?
    Unity document above says this compile error giving behavior depends which .NET profile the Project uses, but you can also see what Roslyn includes by default below. As you see, there is no

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

    https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/csc/csc.rsp

    So you add them manually as the Unity document suggests. I did not check the relation between this behaviour and added third party libraries/plugins myself. So I don't know the reason why it works fine on empty projects. I would be happy if someone else enlightens me.
     
  19. LyceumVR

    LyceumVR

    Joined:
    Apr 16, 2020
    Posts:
    11
    It works for me! Thank you!
     
  20. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    In newer Unity simple set Api Compatibility Level to .NET Standard 2.1 and it just works.
     
  21. lucbloom

    lucbloom

    Joined:
    Mar 27, 2020
    Posts:
    42
    Yes that will work too. I know because I was going the other way when this broke. I was moving some code that should be Editor-only into its rightful place. Somehow the Editor Assembly needs the csc.rsp file.