I've been trying to make a program that, evidently, opens a .zip file and extracts its contents into a .jar file(minecraft.jar, for modding Minecraft), but couldn't find any way to open a .jar file within Java to do so. Is opening a .jar file(not running it) and adding/deleting files within the .jar file possible?
Asked
Active
Viewed 1,852 times
1
-
4a jar and a zip are the same file type. ;) – Breavyn Jul 25 '13 at 07:25
-
3See the [java.util.jar](http://docs.oracle.com/javase/7/docs/api/java/util/jar/package-summary.html) package. Prefer it to the zip package because it has better Unicode entry support. – McDowell Jul 25 '13 at 07:28
-
@ColinGillespie No. JAR is compressed using ZIP but they are not the same file type. Java has own methods to manipulate JARs. No need to treat it as a generic ZIP file. – m0skit0 Jul 25 '13 at 07:31
-
@m0skit0 yes I agree using the `jar` utilities is the better option – Breavyn Jul 25 '13 at 07:35
1 Answers
1
First, @McDowell comment is the root of your solution: the java.util.jar
package is where you'll find the tools you need.
Second, modifying the content of a JAR file on the fly is possible, but complicated and rather a lot of work. Would it not be sufficient for your purposes to open both files (the ZIP and JAR ones, if I understand correctly), extract them in a temporary directory, then re-compress the whole lot?
It seems to me the result would be exactly what you're looking for. It might take a bit of a while to run, but since it's a one-off, I don't feel it's a major issue.

Nicolas Rinaudo
- 6,068
- 28
- 41
-
@Nicholas Yeah that sounds like it'd work. To get all of the contents of the JAR file should I use JarInputStream or JarFile? EDIT: Couldn't I just use JarOutputStream to write to the JAR? – n1ghtk1n9 Jul 25 '13 at 07:53
-
`JarInputStream` and `JarFile` are very similar, I think, and it's mostly up to you: are you working with a `File` or an `InputStream`? If a `File`, I'd go for `JarFile`. As for `JarOutputStream`, I don't believe it supports adding entries to an existing JAR file - you'd use it to write an entire file, not modify an existing one. You might want to check that just in case I'm wrong, though. – Nicolas Rinaudo Jul 25 '13 at 08:09