x# Zipfile lesen

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

User avatar
Horst
Posts: 327
Joined: Tue Oct 13, 2015 3:27 pm

x# Zipfile lesen

Post 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
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

x# Zipfile lesen

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

x# Zipfile lesen

Post by wriedmann »

Hallo Horst,
in den References brauchst Du
System.IO.Compression
System.IO.Compression.FileSystem

Das sollte eigentlich reichen.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

x# Zipfile lesen

Post 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.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
Horst
Posts: 327
Joined: Tue Oct 13, 2015 3:27 pm

x# Zipfile lesen

Post 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
Attachments

[The extension viaef has been deactivated and can no longer be displayed.]

Program.png
Program.png (123.21 KiB) Viewed 1241 times
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

x# Zipfile lesen

Post 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.
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

x# Zipfile lesen

Post 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()
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
Horst
Posts: 327
Joined: Tue Oct 13, 2015 3:27 pm

x# Zipfile lesen

Post 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
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

x# Zipfile lesen

Post 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!)
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

x# Zipfile lesen

Post by robert »

Chris,
You are right, that is a bug.
If you add it to the list, then I will fix it.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply