Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a ZipPath to File?

Tags:

java

path

jaxb

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.

like image 920
Do Re Avatar asked Feb 01 '17 12:02

Do Re


1 Answers

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
like image 154
wheel.r Avatar answered Nov 18 '22 13:11

wheel.r