1

I'm a beginner developer in java. Anyone knows a code how to extract some files from jar archive? Let's say I'm making a installer. I want to extract some files from the jar of my application.

3 Answers 3

1

You can try extracting files like that :

File file = new File("myfilename.ext"); if (!file.exists()) { InputStream link = getClass().getResourceAsStream("/your-path/resources/myfilename.ext"); Files.copy(link, file.getAbsoluteFile().toPath()); // copy files where you want } 
Sign up to request clarification or add additional context in comments.

Comments

0

The jar is just a zip file that contains all the .class files. Open it with any zip software and you can view and extract all the files.

Update

If you want to do unzipping in your Java code. Read this article: http://www.baeldung.com/java-compress-and-uncompress. after unzipping you can do normal file manipulation.

2 Comments

Yeah i know, but i wan't to do it in Java. Inside of my application that will extract it.
Ok, that wasn't clear from the question, I've updated my answer.
0

You may extract files using:

this.getClass().getResourceAsStream("/mydir/myfile.txt"); 

You may then use File.copy() or another to write the file to disk.

The files are typically stored in the /src/main/resources directory if you're using Maven.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.