#2870 Trying to read a resource file from a pod throws UnsupportedErr ZipEntryFile.open

LightDye Thu 26 May 2022

Hi all,

I'm trying to open and read a resource file that is included in a pod, but I'm getting this output from the code below:

File contents: [Hello World!]
File exists: true
sys::UnsupportedErr: ZipEntryFile.open
  fan.sys.ZipEntryFile.open (ZipEntryFile.java:134)
  fan.sys.File.open (File.java:378)
  trypod::Main.main (Main.fan:7)
  java.lang.reflect.Method.invoke (Method.java:498)
  fan.sys.Method.invoke (Method.java:573)
  fan.sys.Method$MethodFunc.callOn (Method.java:244)
  fan.sys.Method.callOn (Method.java:139)
  fanx.tools.Fan.callMain (Fan.java:185)
  fanx.tools.Fan.executeType (Fan.java:147)
  fanx.tools.Fan.execute (Fan.java:41)
  fanx.tools.Fan.run (Fan.java:308)
  fanx.tools.Fan.main (Fan.java:346)

Main.fan

class Main {
  Void main()
  {
    echo( "File contents: " + File( `./src/trypod/res/file.txt` ).open.readAllLines )
    echo( "File exists: " + Pod.of( this ).file( `fan://trypod/res/file.txt` ).exists )
    echo( Pod.of( this ).file( `fan://trypod/res/file.txt` ).open.readAllLines )
  }
}

The file called file.txt exists in the res/ directory and I have included this line in build.fan

resDirs  = [`res/`]

Can anyone please let me know how this is done, or what I'm doing wrong here?

SlimerDude Fri 27 May 2022

Hey LightDye, good to see you back!

I believe the clue is in the err msg, "sys::UnsupportedErr: ZipEntryFile.open" and that you cannot use open() on a Zip file entry.

So instead, try using the read methods on the File object directly:

echo( this.typeof.pod.file(`/res/file.txt`).readAllLines )

Hope this helps,

Steve.

LightDye Fri 27 May 2022

Thanks Steve! It worked. I can't believe it was so simple.

brian Fri 27 May 2022

Just a note: the sys::File.open method opens as a random access file (just like java.io.RandomAccessFile). In general its always better to open as a stream if you can using File.out if you don't need random access.

LightDye Sat 28 May 2022

Thanks Brian for clarifying the difference between open and out.

Login or Signup to reply.