I have in my Java project some JAR files that I need to copy in a temp folder of the filesystem. However, once copied, all copied files are corrupt even their size is not the same as the original (when open zip with 7Zip appears an unformatted file instead of folders structure of jar). I don't need those files for any process in my program like compiling, just for copy to the system.
I have tried some solutions like this, this, or with CommonsIO library, but always the files copied keep corrupt.
EDIT: Some of the codes I've tried are
InputStream source = getClass().getClassLoader().getResourceAsStream(originPath);
Files.copy(source, Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
source.close();
byte[] source = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream(originPath));
File directoryFile = new File(originPath);
if(!directoryFile.exists()) {
directoryFile.mkdirs();
}
try (FileOutputStream fileOutputStream = new FileOutputStream(destPath)){
fileOutputStream.write(source);
}
catch (Exception exception) {
throw new IOException(String.format("Failed to copy the file %s", sourceName));
}
How can I do that?