Reputation: 895
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:
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.
Upvotes: 6
Views: 4210
Reputation: 1
With newer Java versions, you can skip IOUtils
and directly use the Files
class for copying the files out of the ZIP archive to a temp folder. Make sure to replace PREFIX
and SUFFIX
with some values that make sense for your application of the code.
private Path remapZipPath(Path path) {
final File tempFile = File.createTempFile("PREFIX" + UUID.randomUUID(), "SUFFIX");
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
Files.copy(path, out);
}
return tempFile.toPath();
}
Upvotes: 0
Reputation: 211
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
Upvotes: 8