当前位置 : 主页 > 编程语言 > c++ >

java 实现 apk 拆分 合并

来源:互联网 收集:自由互联 发布时间:2021-06-30
java 实现 apk 拆分 合并 import java.io.File;import com.nothome.delta.Delta;import com.nothome.delta.GDiffPatcher;public class AndroidApkUtils {/** * 生成差分包:old_new.patch = diff(old.apk, new.apk) * * */ public static byte[]
java 实现 apk 拆分 合并
import java.io.File;

import com.nothome.delta.Delta;
import com.nothome.delta.GDiffPatcher;

public class AndroidApkUtils {

	/** 
     * 生成差分包:old_new.patch = diff(old.apk, new.apk) 
     *  
     * */  
	public static byte[] createPatch(byte source[], byte target[]) {  
		byte [] fs = null;
        try {  
            Delta d = new Delta();  
            fs = d.compute(source, target);
        } 
        catch (Exception e) {  
            e.printStackTrace();  
        }  
        return fs;
    } 
	
	public static void createPatch(byte source[], byte target[], String patchPath, String patchName) {  
		try {  
			Delta d = new Delta();
			byte [] fs = d.compute(source, target);
			FileUtils.getFile(fs, patchPath, patchName);
        } 
        catch (Exception e) {  
            e.printStackTrace();  
        }
    } 
	

    /**
	 * 合成差分包:new.apk = old.apk + old_new.patch
	 * 
	 * */
	public static File mergeFile(String source, String patch, String target) throws Exception {
		GDiffPatcher patcher = new GDiffPatcher();
		File deffFile = new File(patch);
		File updatedFile = new File(target);
		patcher.patch(new File(source), deffFile, updatedFile);
		return updatedFile;
	}

	public static File mergeApk(String source, String patch, String target, String newApkMd5) throws Exception {
		File updateFile = mergeFile(source, patch, target);
		String ufpMd5 = FileMD5Utils.getFileMD5(updateFile);
		System.out.println("服务端下发的md5:" + newApkMd5 + ",新合并后的apk MD5:" + ufpMd5);
		if (ufpMd5 == null || !newApkMd5.equalsIgnoreCase(ufpMd5)) {
			if (updateFile.exists()) {
				updateFile.delete();
			}
			throw new Exception("MD5错误,不能成功合并!");
		}
		return updateFile;
	}

}
网友评论