简体   繁体   中英

How to extract files from jar file in application?

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.

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.

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM