Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to import 3rd party library / package from source? (SharpZipLib) (doesn't have a .dll)

Discussion in 'Scripting' started by Matsyir, Feb 9, 2022.

  1. Matsyir

    Matsyir

    Joined:
    Mar 13, 2019
    Posts:
    29
    Resolved: NuGet packages are simply .zip files that contain one or more .dll files. Just rename the .nupkg to .zip to retrieve the dll.
    _________________________________________

    Hi all,

    Very straightforward question, title about sums it up. I've been trying to do this simple task for over an hour straight, I'm going insane here, can't believe I still haven't figured this out.

    Basically, I'm just trying to import the SharpZipLib library in order to compress data: https://icsharpcode.github.io/SharpZipLib/

    I cannot get Unity to properly work after importing the library. I can get the library working perfectly fine within my Visual Studio solution, and the code all works fine. But whenever I open Unity again, it'll complain for a variety of reasons.

    Here's what I've tried so far:
    - Installing the library directly in VS through NuGet package manager => "works", but then unity says it can't find the namespace "SharpZipLib" wherever I'm using it, and won't run.
    - Dropping the code's source folder within a subfolder of "Assets" => again, "works" from VS' perspective, but unity complains about not being able to find 999+ names referenced within the source code of the library all over the place.
    - Importing the tarball via unity's package manager => not too sure what happens here but it doesn't work at all.

    Most guides I've come across related to this talk about importing a .dll library, but this one is not a DLL. It is a folder/zip/tarball containing the entire source code of the library. Importing the source folder the same way people are importing .dlls does not seem to work either.
     
    Last edited: Jun 3, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,039
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,114
    I used the SharpZipLib in the past without any issues, though I haven't tried in the recent years. Since it's a nicely brushed up library you should not include the source files in your project but just the compiled dll file. There's no point to compile the whole library into the assembly that Unity creates out of your own scripts.

    When you copy the DLL into your project, make sure the import settings are setup correctly so it's used on all platforms (including the editor). Sometimes Unity does not update the C# project file correctly that there's a new assembly in the project. If you check the "references" inside VisualStudio and you can not find the "ICSharpCode.SharpZipLib" assembly, go to Unity, right click on any asset and choose "Open C# Project". That should rebuild the "csproj" file and make sure the references are setup correctly. When it shows up in the references, Visual studio should be able to see the namespaces and Unity should compile your code just fine.

    I just downloaded the DLL, put it into my test project, make sure it imported correctly as I just explained and did a quick test.

    Code (CSharp):
    1.  
    2. using ICSharpCode.SharpZipLib.Zip;
    3.  
    4. // [ ... ]
    5.     private void Awake()
    6.     {
    7.         var zip = new ZipFile("C:\\data\\test.zip");
    8.         foreach (ZipEntry item in zip)
    9.         {
    10.             if (item.IsFile)
    11.                 Debug.Log("file: " + item.Name + " Size:" + item.Size);
    12.             else if (item.IsDirectory)
    13.                 Debug.Log("directory: " + item.Name + " Size:" + item.Size);
    14.             else
    15.                 Debug.Log("unknown: " + item.Name);
    16.         }
    17.     }
    18.  
    It happily reads the content of that test zip file and prints out the filenames.
     
    Matsyir likes this.
  4. Matsyir

    Matsyir

    Joined:
    Mar 13, 2019
    Posts:
    29
    Update: Finally managed to get the SharpZipLib .dll file: NuGet packages are simply .zip files that contain one or more .dll files. Just rename the .nupkg to .zip to retrieve the dll. How this was so hard to figure out is beyond me.
    ______________________________

    Ok, it looks like this is pretty much what I need to do. But the problem is that I simply cannot find the DLL version of SharpZipLib, only source code. Did you have to compile the DLL yourself? Do you have any idea where you found the .dll file? I have gone through every inch of the github + the website, and there is only a download to:
    - nuget package file
    - source code (.zip)
    - source code (.tar.gz)

    Sorry, this all feels really silly, I'm not too familiar with generating .dll files, and I can't believe I'm having such a hard time finding a download link to SharpZipLib's .dll if that's how c# libs are meant to be referenced.... I quickly tried opening the entire project in VS and compiling the .dll myself, but there were plenty of build errors.

    This is what the folder structure of my downloaded SharpZipLib source looks like:
    README.md does not refer to any packaged .dll file. I've gone through nearly every folder and can't find a .dll, it's all straight .cs source files.
    upload_2022-2-9_14-25-52.png


    My issue at this point is literally just figuring out where the hell the SharpZipLib .dll file is. Or how to properly compile it myself.

    While I'm sure I can figure out a solution with one of these existing packages, it would be good to know how to properly import libraries for the future, I'm sure this won't be the only one I'll want to use. I've also literally written the code I needed SharpZipLib for, using SharpZipLib functions, I just can't get the Unity project/editor to properly recognize the library, only VS.

    ______________________________

    Update: Finally managed to get the SharpZipLib .dll file: NuGet packages are simply .zip files that contain one or more .dll files. Just rename the .nupkg to .zip to retrieve the dll. How this was so hard to figure out is beyond me.
     
    Last edited: Jun 3, 2023
    DERKLORD and DhiaSendi like this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,114
    Exactly ^^. I'm not a huge fan of most packet managers myself. Maybe I'm just not used to them. Anyways as you already figured out a nuget package is just an archive that can be opened with any zip program or when renamed to .zip so the windows explorer can open them as well. In the past the SharpZipLib was still downloadable as just a single dll file. While I can understand that the package managers can be useful in some cases, especially with dependencies, it would be great if there was still an alternative download for them.
     
    DhiaSendi and Matsyir like this.
  6. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,743
    Last edited: Jun 3, 2023
    CodeSmile, Matsyir and Kurt-Dekker like this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,481
    It's a wrapper for it and as of right now is based off of an older release (1.3.1) rather than the latest (1.4.2).
     
  8. Matsyir

    Matsyir

    Joined:
    Mar 13, 2019
    Posts:
    29
    FWIW, I did get this working as I needed with SharpZipLib, but I ended up replacing it with the 'native' GZipStream found in System.IO.Compression, simply to remove the additional dependency (basically same result in my case).

    However, depending on your use case, SharpZipLib or other packages may be a better fit.
     
    Last edited: Jun 3, 2023
  9. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,743
    A plugin instead of directly using a DLL like that saves you headache when trying to deploy to non-Windows platforms at least.
     
    Matsyir likes this.
  10. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,505
    Would be interesting to hear about that use case. It would have to be very compelling to ditch built-in GZipStream. Did anyone measure performance?