Can anyone recommend a Java library to manipulate jar files? I am finding the facilities available at java.util.jar rather terse (or maybe I don’t have some example code to look at). I am interested in things like:
- adding files to a jar
- removing files from a jar
- create a jar file from a folder
- deflate a jar file to a folder
- reading the contents of a jar entry (perhaps in memory, without having to deflate the jar on disk)
- creating a jar entry in memory without having to read from a file on disk.
- asking whether a jar file entry is a Manifest file or some other special file or not.
If anyone can point to example code using either the java.util.jar or some other library that would help. In particular looking at the classes and methods of java.util.jar it is not clear to me how to read the contents of a particular Jar entry or even how to deflate the jar (without spawning an external process).
1 & 2 are simple manual operations of copying the entries from Jar to another adding or leaving the entries you want, deleting he original file and renaming the new to replace it.
As jtahlbom has pointed out, the Java Jar API handles the rest out of the box.
UPDATE with example
These are REALLY basic examples. They show you how to read and write a Jar. Basically from that you can derive just about everything else you need to do.
I’ve setup a personal library (not included) which basically allows me to pass in an
InputStreamand have it written to anOutputStreamand visa versa. This means you could read from any where and write to anywhere, which could cover most of you requirements.If you take the time look, there are a number of examples of how to add/remove entries from the from zip/jar files on SO