xsharp.eu • x# Zipfile lesen
Page 1 of 2

x# Zipfile lesen

Posted: Thu Mar 02, 2023 4:30 pm
by Horst
Hallo
Ich scheitere wieder einmal an einer fehlenden Resource weiss aber nicht was der Compiler will.

error XS0234: The type or namespace name 'FileSystem' does not exist in the namespace 'System.IO.Compression' (are you missing an assembly reference?) 1,1 Start.prg
Compilation failed (1 error)

Code: Select all

#using System.IO
#using System.IO.Compression
#using System.IO.Compression.FileSystem

FUNCTION TestZipFile () AS VOID
	
	LOCAL cZipFile := "ubs.zip"	AS STRING  
	LOCAL oArchive			AS ZipFile

	oArchive := ZipFile:OpenRead(WorkDir()+cZipFile)

	FOREACH VAR entry IN oArchive:Entries
	   	Console.WriteLine(entry:Name)
	NEXT	
	
	RETURN
Gruss
Horst

x# Zipfile lesen

Posted: Thu Mar 02, 2023 5:31 pm
by robert
Horst,

What happens if you comment out that line?

BTW: in X# we use USING instead of #using. We still support #using for compatibility with Vulcan, but it is really not a preprocessor command like #define and #ifdef.

Robert

x# Zipfile lesen

Posted: Thu Mar 02, 2023 7:11 pm
by wriedmann
Hallo Horst,
in den References brauchst Du
System.IO.Compression
System.IO.Compression.FileSystem

Das sollte eigentlich reichen.

Wolfgang

x# Zipfile lesen

Posted: Thu Mar 02, 2023 7:33 pm
by Chris
Guys,

To combine your answers, "System.IO.Compression.FileSystem" is not a namespace, it is a dll. So Horst you need to add a reference to this library and remove that #using from your code.

x# Zipfile lesen

Posted: Fri Mar 03, 2023 7:32 am
by Horst
Guten Morgen
Ich habe die Dll eingebunden. Im Anhang (Bild) mein erster c# Versuch mit demselben Fehler. Danach habe ich es in x# versucht.
Habe jetzt eine eigene viaef (myZipFile) gemacht und anghängt.

Robert: if i comment out the 'using FileSystem' , i have more errors.
When i add the dll System.IO.Compression.ZipFile, then i have the same one error like bevor.

Code: Select all

error XS0723: Cannot declare a variable of static type 'System.IO.Compression.ZipFile'	1162,2	Start.prg	TestZipFile
error XS0029: Cannot implicitly convert type 'System.IO.Compression.ZipArchive' to 'System.IO.Compression.ZipFile'	1164,2	Start.prg	TestZipFile
error XS1061: 'System.IO.Compression.ZipFile' does not contain a definition for 'Entries' and no accessible extension method 'Entries' accepting a first argument of type 'System.IO.Compression.ZipFile' could be found (are you missing a using directive or an assembly reference?)	1166,2	Start.prg	TestZipFile

x# Zipfile lesen

Posted: Fri Mar 03, 2023 8:27 am
by FFF
Horst,
es hängt nur an einer Zeile ;-)
-> LOCAL oArchive AS ZipArchive
Archive, nicht File, dann läufts.

"USING System.IO.Compression.FileSystem" kann nicht compilieren, wie ein Blick in https://learn.microsoft.com/en-us/dotne ... ew=net-7.0 zeigt, gibt es kein "Filesystem" in dieser Klasse.

x# Zipfile lesen

Posted: Fri Mar 03, 2023 8:36 am
by Chris
Hi Horst,

This is why I hate the VAR command, because it hides the information about the names of the types used. OpenRead() is a STATIC (not instance) method of the ZipFile class (basically it's just like a common function), so you need to use the dot to use it, not a colon (you must use a colon when you are calling a method of an instance, ie in a local, but here, there's no local). This method returns an instance of the "ZipArchive" class.

Additionally, the "using" you see in the middle of the code, is a very different thing to the "using"s in the beginning of the file (yes, unfortunately c# uses "using" for so many different things, like it also does with the dot operator), it's a code construct to automatically restrict visibility of the local and call an object's Dispose() method at the end of the construct. Direct equivalent of the code in X# would be (removing the unnecessary stuff):

Code: Select all

USING System.IO.Compression
FUNCTION Start( ) AS VOID
	LOCAL cZipFile := "ubs.zip"	AS STRING  
	BEGIN USING LOCAL oArchive := ZipFile.OpenRead(WorkDir()+cZipFile) AS ZipArchive
		FOREACH entry AS ZipArchiveEntry IN oArchive:Entries
		   	Console.WriteLine(entry:Name)
		NEXT	
	END USING
In order to make the code a bit easier to understand, it could be expanded to the almost identical:

Code: Select all

USING System.IO.Compression
FUNCTION Start( ) AS VOID
	LOCAL cZipFile := "ubs.zip"	AS STRING  
	LOCAL oArchive AS ZipArchive
	oArchive := ZipFile.OpenRead(WorkDir()+cZipFile)
	FOREACH entry AS ZipArchiveEntry IN oArchive:Entries
	   	Console.WriteLine(entry:Name)
	NEXT	
	oArchive:Dispose()

x# Zipfile lesen

Posted: Fri Mar 03, 2023 10:59 am
by Horst
Hallo Chris and Karl

Chris , i hate it also ;-) i dont see what it is and i have to make research to find out and my experience with c# is not much . I did not know what 'entry' is so i let the VAR in the code.
BTW.

Code: Select all

ZipFile:OpenRead(WorkDir()+cZipFile)
works too.
My Function now:

Code: Select all

FUNCTION Start( ) AS VOID
	LOCAL cZipFile := "ubs.zip"	AS STRING  
	LOCAL oArchive				AS ZipArchive 
	LOCAL entry					AS ZipArchiveEntry 

	oArchive := ZipFile.OpenRead(WorkDir()+cZipFile)

	FOREACH entry IN oArchive:Entries
	   	Console.WriteLine(entry:Name)
	NEXT	
oArchive:Dispose()	
RETURN
Karl, leider sehe ich im MS Help gar nichts :-) Da habe ich einfach zu wenig Erfahrung wie die Infos in diesem Help zusammengesetzt sind. Erst nachdem ich in allen (ZipArchiv, ZpiArchiveEntry) herumgeschnüffelt habe (und dem Text von Chris) sehe ich die Zusammenhänge. Aber warum alles so kompliziert aufgeteilt ist, anstatt in einer Klasse kapier ich nicht.

Danke für eure Hilfe

x# Zipfile lesen

Posted: Fri Mar 03, 2023 12:07 pm
by Chris
Hi Horst,
Horst post=25524 userid=315 wrote: BTW.

Code: Select all

ZipFile:OpenRead(WorkDir()+cZipFile)
works too.
Oh, thanks, that's definitely a compiler bug, should not had allowed it. Please do use the dot in this case, as this will be throwing an error in the next build (unless Robert disagrees!)

x# Zipfile lesen

Posted: Fri Mar 03, 2023 12:09 pm
by robert
Chris,
You are right, that is a bug.
If you add it to the list, then I will fix it.

Robert