Welcome, Guest
Username: Password: Remember me
Hier wird Deutsch gesprochen
  • Page:
  • 1

TOPIC:

x# Zipfile lesen 02 Mar 2023 17:30 #25506

  • Horst
  • Horst's Avatar
  • Topic Author


  • Posts: 293
  • 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)
    #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

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 02 Mar 2023 18:31 #25507

    • robert
    • robert's Avatar


  • Posts: 3599
  • 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

    Please Log in or Create an account to join the conversation.

    Last edit: by robert.

    x# Zipfile lesen 02 Mar 2023 20:11 #25509

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 02 Mar 2023 20:33 #25512

    • Chris
    • Chris's Avatar


  • Posts: 3981
  • 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.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 08:32 #25520

    • Horst
    • Horst's Avatar
    • Topic Author


  • Posts: 293
  • 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.
    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

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 09:27 #25522

    • FFF
    • FFF's Avatar


  • Posts: 1419
  • 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 learn.microsoft.com/en-us/dotnet/api/sys...ression?view=net-7.0 zeigt, gibt es kein "Filesystem" in dieser Klasse.
    Regards
    Karl (X# 2.15.0.3; Xide 2.15; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 09:36 #25523

    • Chris
    • Chris's Avatar


  • Posts: 3981
  • 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):

    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:

    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()
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Last edit: by Chris.

    x# Zipfile lesen 03 Mar 2023 11:59 #25524

    • Horst
    • Horst's Avatar
    • Topic Author


  • Posts: 293
  • 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.
    ZipFile:OpenRead(WorkDir()+cZipFile)
    works too.
    My Function now:
    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

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 13:07 #25526

    • Chris
    • Chris's Avatar


  • Posts: 3981
  • Hi Horst,

    BTW.
    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!)
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 13:09 #25527

    • robert
    • robert's Avatar


  • Posts: 3599
  • 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

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 14:55 #25531

    • FFF
    • FFF's Avatar


  • Posts: 1419
  • Horst,
    i hear you :-) und bin ganz der gleichen Meinung.

    Jedenfalls, als Tipp: Ich habe Dein Beispiel in Xide geholt, compiliert, die Fehler angesehen.
    (Xide hat in Tools/Preferences/Advanced zwei SLEs für die Hilfe, im ersten steht bei mir:
    learn.microsoft.com/en-us/dotnet/api/%value%
    im zweiten
    www.google.com/?gws_rd=ssl#q=%value%)

    Cursor auf "Zipfile" gesetzt, F1, Firefox öffnet mit sich auf
    learn.microsoft.com/en-us/dotnet/api/Sys...ZipFile?view=net-7.0
    und erstmal etwas gelesen.
    Unter Remarks ist das " you must add a reference to the System.IO.Compression.FileSystem assembly in your project." drin, deshalb hast Du ja auch die Dll referenziert. Das ist aber der NAME der Dll. Nur MS weiß, warum das mitnichten bedeuten muss, dass es innen drin einen Typ (grob: eine Klasse) gleichen Namens geben wird - in diesem Fall gibt es ihn nicht, ergo der Compilerfehler, beim "USING System.IO.Compression.FileSystem". Da das "using" ja nur eine Bequemlichkeit ist, um nicht im Code die Langnamen schreiben zu müssen, habe ich es erstmal auskommentiert, um zu sehen, was dann passiert.

    Erst kriegst Du den Static-Fehler um die Ohren, aber der zweite Fehler ist schon, das er nicht implizit aus einem Ziparchive ein Zipfile machen kann. Daraufhin hab ich das einfach mal in der Deklaration umgedreht, schwupp, gings.

    Nein, nicht wirklich befriedigend, aber was ist schon perfekt. Man hangelt sich halt so hin ;-)
    Regards
    Karl (X# 2.15.0.3; Xide 2.15; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    x# Zipfile lesen 03 Mar 2023 17:06 #25533

    • Horst
    • Horst's Avatar
    • Topic Author


  • Posts: 293
  • Falls mal einer das brauchen kann.

    Öffnet ein Zipfile und liest die einzelnen 'text' files in einen string.
    USING System
    USING System.Text
    USING System.IO
    USING System.IO.Compression
    
    FUNCTION Start( ) AS VOID
    	LOCAL cZipFile := "ubs.zip"	AS STRING  
    	LOCAL oArchive			AS ZipArchive 
    	LOCAL entry			AS ZipArchiveEntry 
    	LOCAL cBuffer		        AS STRING
    
    	oArchive := ZipFile.OpenRead(WorkDir()+cZipFile)
    
    	FOREACH entry IN oArchive:Entries
    	   	Console.WriteLine(entry:Name)     
    		cBuffer := System.IO.StreamReader{ entry:open (),System.Text.Encoding.UTF8}:ReadToEnd()
    		Console.WriteLine(cBuffer)     
    	NEXT	
    	oArchive:Dispose()	
    
    	RETURN 	  

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1
    Moderators: wriedmann