# How to do binary diffs with Java jars This is a simple example how to generate and apply a binary patch to a jar file. In the hope that some tooling vendor or gradle / maven / repository backend guru might pick this up to unban us from too long dependency download times :) # Download test jars: ``` $ wget http://central.maven.org/maven2/com/google/guava/guava/18.0/guava-18.0.jar $ wget http://central.maven.org/maven2/com/google/guava/guava/16.0.1/guava-16.0.1.jar ``` # Abtain bsdiff / bspatch: http://www.daemonology.net/bsdiff/ #Create the binary patch file ``` $ bsdiff guava-16.0.1.jar guava-18.0.jar guava-16.0.1-18.0.patch ``` #List directory: ``` $ ll total 10520 -rw-r--r-- 1 tom staff 878K Mar 12 10:45 guava-16.0.1-18.0.patch -rw-r--r-- 1 tom staff 2.1M Feb 3 2014 guava-16.0.1.jar -rw-r--r-- 1 tom staff 2.2M Aug 25 2014 guava-18.0.jar ``` As one can see the binary patch file is aroung 900kb #Rename the original jar file ``` $ mv guava-18.0.jar guava-18.0.jar.orig ``` #compute the MD5 hash of the original file ``` $ md5 guava-18.0.jar.orig MD5 (guava-18.0.jar.orig) = 947641f6bb535b1d942d1bc387c45290 ``` # Apply the binary patch file ``` $ bspatch guava-16.0.1.jar guava-18.0.jar guava-16.0.1-18.0.patch ``` # List the directory again ``` $ ll total 14928 -rw-r--r-- 1 tom staff 878K Mar 12 10:45 guava-16.0.1-18.0.patch -rw-r--r-- 1 tom staff 2.1M Feb 3 2014 guava-16.0.1.jar -rw-r--r-- 1 tom staff 2.2M Mar 12 10:51 guava-18.0.jar -rw-r--r-- 1 tom staff 2.2M Aug 25 2014 guava-18.0.jar.orig ``` #Compute the MD5 hash of the newly created file ``` $ md5 guava-18.0.jar MD5 (guava-18.0.jar) = 947641f6bb535b1d942d1bc387c45290 ``` The checksums match as one can see :) I think it would be a nice feature and a huge bandwith saver if maven / gradle and the repository backends would support binary diff for jars.