0

I want to include a folder in my project (which will be a JAR or a plugin) to copy&paste it somewhere at runtime. I tried the following:

private static void copyStream(InputStream i, OutputStream o) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while((bytesRead = i.read(buffer)) != -1)
        o.write(buffer, 0, bytesRead);
}

InputStream is = getClass().getResourceAsStream(src);
FileOutputStream fos = new FileOutputStream(dest); // error
copyStream(is, fos);

But I got a java.io.FileNotFoundException "Access denied" because a folder can't be written like that. If it was a file, it would work.

Note: How can I get a resource "Folder" from inside my jar File? didn't solve my problem because I don't want to list the contained files, I only want to copy&paste the whole folder. java.io.FileNotFoundException: (Access is denied) didn't either because I don't want to read the folder dest but to write it from a stream, since the embedded folder src can't be referenced in a File object.

Community
  • 1
  • 1
Codoscope
  • 892
  • 1
  • 10
  • 18
  • You can try `FileUtils` from Apache common? – akhil_mittal Sep 23 '15 at 08:21
  • short answer: it doesn't work that way. resources in JARs are not files or folders. but if you open your jar using a compression tool like winzip or MC, you can probably manually extract the "folder" – Sean Patrick Floyd Sep 23 '15 at 08:44
  • I tried FileUtils with copyURLToFile but I figured out I could'nt use it because in my code, "is" is actually set to null. The method of using a zip worked (I had to copy&paste the zip outside and then unzip and delete it) although it is quite a dirty solution. – Codoscope Sep 23 '15 at 13:22

0 Answers0