介绍
ota差分算法,常见的有bsdiff,hdiffpatch,xdelta3这三种,主要是解决ota升级中升级包更新推送升级包不必全量推送的问题。比如,一个旧的升级包100M,新的升级包110M,通过差分算法工具云端可以把两个包之间有差异的地方抽离出来新生成一个比较小的差分包,云端只要向客户端推送这个比较小的差分包,客户端拿到差分包之后,再通过差分算法工具利用旧包和拿到的差分包还原成新的升级包实现升级包的替换安装。这三种差分算法都可以完成这个步骤,其中hdiffpatch是性能比较好、占用内存比较少的方案。
HDiffPatch安装
github地址:https://hub.fastgit.org/sisong/HDiffPatch
安装包下载地址:https://hub.fastgit.org/sisong/HDiffPatch/releases/tag/v4.1.2
截止发文最新版本是v4.1.2,下载对应 hdiffpatch_v4.1.2_bin_linux64.zip即可。
解压后按照github上readerme.md里描述的方法安装并编译即可:
cd <dir>/HDiffPatch
make LZMA=0 ZSTD=0 MD5=0
差分命令:hdiffz oldPath newPath outDiffFile (oldPath:旧升级包路径,newPath:新升级包路径,outDiffFile:将要生成的差分包文件路径)
还原命令:hpatchz oldPath diffFile outNewPath(oldPath:旧升级包路径,diffFile:差分包路径,还原生成的升级包路径)
演示
比如我的hdiffpatch安装路径为/home/zhaohy/hdiffpatch/HDiffPatch
在/home/zhaohy/hdiffpatch/test-20211116文件下有两个文件分别是:10M_old.zip、10M_new.zip代表旧升级包和新升级包。
差分:
zhaohy@zhaohy-VirtualBox:~/hdiffpatch/HDiffPatch$ cd /home/zhaohy/hdiffpatch/HDiffPatch
zhaohy@zhaohy-VirtualBox:~/hdiffpatch/HDiffPatch$ ./hdiffz /home/zhaohy/hdiffpatch/test-20211116/10M_old.zip /home/zhaohy/hdiffpatch/test-20211116/10M_new.zip /home/zhaohy/hdiffpatch/test-20211116/10M_diff.zip
old : "/home/zhaohy/hdiffpatch/test-20211116/10M_old.zip"
new : "/home/zhaohy/hdiffpatch/test-20211116/10M_new.zip"
out : "/home/zhaohy/hdiffpatch/test-20211116/10M_diff.zip"
hdiffz run with compress plugin: ""
oldDataSize : 12382208
newDataSize : 11178603
diffDataSize: 231050
diff time: 1.821 s
out diff file ok!
load diffFile for test by patch:
diffDataSize: 231050
patch time: 0.003 s
patch check diff data ok!
all time: 1.825 s
可以看到生成差分包成功。
还原:
zhaohy@zhaohy-VirtualBox:~/hdiffpatch/HDiffPatch$ ./hpatchz /home/zhaohy/hdiffpatch/test-20211116/10M_old.zip /home/zhaohy/hdiffpatch/test-20211116/10M_diff.zip /home/zhaohy/hdiffpatch/test-20211116/10M_out_new.zip
old : "/home/zhaohy/hdiffpatch/test-20211116/10M_old.zip"
diff: "/home/zhaohy/hdiffpatch/test-20211116/10M_diff.zip"
out : "/home/zhaohy/hdiffpatch/test-20211116/10M_out_new.zip"
oldDataSize : 12382208
diffDataSize: 231050
newDataSize : 11178603
patch ok!
hpatchz time: 0.030 s
可以看到还原成功,比对10M_new.zip和10M_out_new.zip发现他们两个是一样的。
java调用动态库libhdiffpatch.so
把hdiffpatch源文件编译成so动态库文件,编译过程就不写了(C++编译我也不会),这里放一个大神编译好的so文件下载地址吧:libhdiffpatch.so
把libhdiffpatch.so文件放在src/main/resource/linux-x86-64/文件夹下
gradle引入jna
compile "net.java.dev.jna:jna:5.7.0"
创建HDiff类
package hdiffpatch.core;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface HDiff extends Library{
HDiff INSTANCE = (HDiff) Native.load("hdiffpatch", HDiff.class);
//diff usage: hdiffz [options] oldPath newPath outDiffFile
void hdiffz(String oldData, String newData, String outPatchFile, long size);
}
创建HPatch类
package hdiffpatch.core;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface HPatch extends Library {
HPatch INSTANCE = (HPatch) Native.load("hdiffpatch", HPatch.class);
//patch usage: hpatchz [options] oldPath diffFile outNewPath
void hpatchz(String oldPath, String diffFile, String outNewPath);
}
创建调用类HdiffPatch
package hdiffpatch.core;
import java.io.FileNotFoundException;
import ly.mp.iov.bsdiff.util.FileUtils;
/**
* 二进制文件差分Java版
*/
public class HdiffPatch {
/**
* 差分
* @param oldFile 原始文件
* @param newFile 目标文件
* @param patchFile 差分文件
* @throws FileNotFoundException oldFile或newFile不存在
*/
public static void diff(String oldFile, String newFile, String patchFile) throws FileNotFoundException {
FileUtils.requireFileExist(oldFile);
FileUtils.requireFileExist(newFile);
HDiff.INSTANCE.hdiffz(oldFile,newFile, patchFile, 1024*1024*128);
}
/**
* 还原
* @param oldFile 原始文件地址
* @param diffPath 差分文件地址
* @param outNewPath 还原新生成文件地址
* @throws FileNotFoundException oldFile或patchFile不存在
*/
public static void patch(String oldPath, String diffPath, String outNewPath) throws FileNotFoundException {
FileUtils.requireFileExist(oldPath);
FileUtils.requireFileExist(diffPath);
HPatch.INSTANCE.hpatchz(oldPath, diffPath, outNewPath);
}
public static void main(String[] args) {
//System.out.println(System.getProperty("java.library.path"));
String path = "/home/zhaohy/hdiffpatch/test-20211116/";
String oldFile = path + "10M_old.zip";
String newFile = path + "10M_new.zip";
String patchFile = path + "10M_diff.zip";
String outNewPath = path + "10M_out_new.zip";
try {
HdiffPatch.diff(oldFile, newFile, patchFile);//差分
HdiffPatch.patch(oldFile, patchFile, outNewPath);//还原
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(patchFile);
}
}
贴上FileUtils类:
package hdiffpatch.util;
import java.io.File;
import java.io.FileNotFoundException;
/**
* 文件工具类
*/
public class FileUtils {
/**
* 校验文件是否存在,如果不存在则抛出异常
* @param filePath 文件路径
* @throws FileNotFoundException 文件不存在
*/
public static void requireFileExist(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
}
}
如此运行main方法也是可以正常差分和还原包的。完结散花~