Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Difference in Zip/Jar file

Tags:

java

jar

zip

It seems like building a jar or zip from the exact same source files will always yield a different file. I tried this both using the java jar command, and the jar and zip tasks in ant.

It appears to be due to the fact that new jars/zips have the timestamp set to the current time on each stored file.

Is there a way to force a zip tool to simply use the timestamp on the file on the filesystem to ensure that a jar built from the exact same source will appear exactly the same?

like image 385
Mike Miller Avatar asked Apr 15 '09 20:04

Mike Miller


People also ask

What is difference between jar and ZIP?

JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java.

Is a JAR file considered a binary?

Jar file contains compiled Java binary classes in the form of *. class which can be converted to readable . java class by decompiling it using some open source decompiler. The jar also has an optional META-INF/MANIFEST.

How can you tell the difference in jars?

If you want to compare two zip files, you can use Java native libraries like ZipEntry. Or you can use any 3rd party utilities like Apache. You can get the list of files in one jar file and see if all the files are present in the other jar file. This way you will know the delta of the two jar files.

Can you convert ZIP to jar?

Generally if your ZIP contains the class files (And not the sources): Just rename the file, a jar is a zip file. But if you're aiming for a class to be launched with command java -jar myProject. jar you should create a MANIFEST file containing the main-class and libraries to use in the classpath.


1 Answers

The binary difference is because of the timestamp of the manifest files. If you let jar create a manifest itself it will create a manifest on the fly and set the created manifest to currentTimeMillis.

You can solve it by:
1. Do not add a manifest (if your using ant you must use zip instead of jar)
2. Add the manifest like you add normal files. (So the manifest is a file on your filesystem and it isn't created on the fly)

like image 190
Kees Avatar answered Oct 06 '22 21:10

Kees