mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-08 21:21:57 +00:00
Some functions of sharpcompress don't work when Reflection is disabled in NativeAOT #622
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @klimatr26 on GitHub (Apr 9, 2024).
Hello.
I have been trying to include sharpcompress in a NativeAOT application. It normally works fine.
However, when I disable Reflection via
<IlcDisableReflection>true</IlcDisableReflection>, some parts stop working and throw and exception related to Reflection being disabled.I used the "ZipArchive with Writing API Example" from the wiki for one program with Reflection disabled and it seemed to work fine. However, using other code like the "Reader (forward-only streams) API Example" example throws an exception when Reflection is disabled (but works fine when Reflection is enabled in NativeAOT).
I know this only affects a very small number of users, since JIT always should support Reflection, and NativeAOT supports enough Reflection features to use sharpcompress without issue. However, disabling Reflection reduces the size of the resulting executable significantly, and that's why I was trying to use that mode.
Have a good day.
@adamhathcock commented on GitHub (Apr 10, 2024):
I'm happy to rework this to support this scenario, I just don't know what is needed. Is it just a matter of putting that in the csproj? Because it builds for me.
@klimatr26 commented on GitHub (Apr 10, 2024):
Hello.
The
<IlcDisableReflection>true</IlcDisableReflection>switch is enabled by the programmer in the csproj of their program, if they want to publish with AOT and disable reflection.I got this exception when trying to list the contents of a ZIP file from a Task:
Does this mean the problem actually exists in System.Text?
Also, the code for listing the contents of the ZIP file is inside a try-catch block, so... ???????. BUT ALSO, the program was working without Reflection until I added SharpCompress, and the rest of the program still works correctly without Reflection (except for the ZIP listing part).
However, I don't understand why a similar program that creates 2 zip archives (one Deflate and one LZMA), lists the contents of one of them, and extracts one of them, works correctly with Reflection disabled.
I'm not sure if it is related to being run from a Task, because I remember that I got a similar Exception when I tried to list and extract the contents of a 7-zip archive -which cannot be opened in Windows 11 (maybe they forgot to enable LZMA in libarchive?)- which normally can be extracted fine by SharpCompress.
UPDATE: I tried the previously problematic code for extracting the 7z archive, and it worked fine without Reflection.
At this point, I'm REALLY confused. Maybe this isn't related to SharpCompress? It is weird that it only happens in (async) code that calls SharpCompress. I saw that you already replaced Activator calls with delegates.
Unrelated: Does SharpCompress load the entire solid 7z block to memory? Memory usage increases beyond the sliding window when I extract solid entries. 7zdec from the LZMA SDK also does this.
Also unrelated: SharpCompress marks the minimum version for extracting LZMA entries of a ZIP file as 20 instead of 63. The Writer code doesn't seem to have any checks for this.
Have a good day.
@adamhathcock commented on GitHub (Apr 10, 2024):
there's a lot to unpack here
there could be a reference leak with 7z/LZMA but don't know, haven't tested. I don't load everything into memory other than that the file has to be random access.
LZMA minimum version is also something I haven't looked into
@klimatr26 commented on GitHub (Apr 10, 2024):
Hello.
I did more testing, and I have some code.
The following code throws an Unhandled Exception even inside a try-catch:
However, simply removing the
//fromConsole.WriteLine("Try to create Reader");seems to solve the issue. (Maybe should close the issue? I still don't understand why this happens specifically with SharpCompress) The program then behaves normally.This is REALLY weird to me, and I don't think SharpCompress is the problem here.
My csproj contains
Sorry to bother you with this.
Have a good day.