6

In order to work with Jaxb, I need to have a normal java.io.File Object. As I do not want to have legacy code in a quite new project, I want to use java.nio.file.Path objects.

As gradle resolves dependencies in jar files, I need to handle them as com.sun.nio.zipfs.ZipPath. Now the thing is, that I am using these files only for codegeneration, and do not necessarily want to unpack them.

Unfortunately, the ZipPath.toFile() method throws an UnsupportedOperationException, so I cannot convert the Path to a File, which is necessary in order to use the JaxbUnmarshaller, to validate the correct layout of the file and to convert it into an actual runtime object.

I tried:

  • ZipPath.toFile(); resulted in error
  • Paths.get(ZipPath.toUri()).toFile(); resulted in error as the result is a ZipPath again

How can I get a File from a ZipPath without unzipping it?

I suppose it is possible via the ZipFileSystem, but I dont get it.

1
  • show your code at first, what you did already Feb 1, 2017 at 13:34

1 Answer 1

8

It's not lovely but I recently solved this problem like so:

InputStream is = Files.newInputStream(zipPath);
final File tempFile = File.createTempFile("PREFIX", "SUFFIX");
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile))
{
    IOUtils.copy(in, out);
}
// do something with tempFile
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.