0

In extension to this question.

Is it possible to read a file into a string without knowing the path to the file? - I only have the file as a 'def'/type-less parameter, which is why I can't just do a .getAbsolutePath()

To elaborate on this, this is how I import the file (which is from a temporary .jar file)

def getExportInfo(path) {
  def zipFile = new java.util.zip.ZipFile(new File(path))

  zipFile.entries().each { entry ->
    def name = entry.name
    if (!entry.directory && name == "ExportInfo") {
      return entry
    }
  }
}
Jonas Praem
  • 2,296
  • 5
  • 32
  • 53
  • What do you mean by "the file"? Is it a `File` object that you have in that type-less parameter, then it is the same. Groovy is duck-typing, so if it is `File` object, you can use it as a `File` object, even if the variable type is undefined. What exactly do you have? – Vampire Jun 09 '17 at 10:14
  • @Vampire No it is not a file object, edited to explain – Jonas Praem Jun 09 '17 at 10:19
  • @JonasPraem, what is a `type-less parameter` ? and why did you put in your question zip file reding? can you explain what do you need? – daggett Jun 09 '17 at 10:32
  • So you want to read the contents of `entry` or what? – Vampire Jun 09 '17 at 10:36
  • @Vampire yes, I read the content of entry – Jonas Praem Jun 09 '17 at 10:56

1 Answers1

1

A ZipEntry is not a file, but a ZipEntry.

Those have almost nothing in common.

With def is = zipFile.getInputStream(entry) you get the input stream to the zip entry contents.

Then you can use is.text to get the contents as String in the default platform encoding or is.getText('<theFilesEncoding>') to get the contents as String in the specified encoding, exactly the same as you can do on a File object.

Vampire
  • 35,631
  • 4
  • 76
  • 102