package com.mvc.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 生成差分文件工具类
 */
public class DiffUtil {
    private final static String BSDIFF_PATH = "E:/xzeai_download/upgrade/bsdiff.exe";

    /**
     * 生成差分文件
     * @param oldFile
     * @param newFile
     * @param diffFile
     * @return
     */
    public static boolean generateDiff(String oldFile, String newFile, String diffFile) {
        StringBuilder builder = new StringBuilder();
        try {
            // 第三方程序的完整路径
            String[] command = {BSDIFF_PATH, "b", oldFile, newFile, diffFile}; // Windows

            // 创建ProcessBuilder实例
            ProcessBuilder pb = new ProcessBuilder(command);

            // 启动命令
            Process process = pb.start();

            // 获取输出和错误输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = reader.readLine()) != null) {
                System.err.println(line);
            }

            // 等待命令完成
            int exitCode = process.waitFor();
            System.out.println("Command exited with code: " + exitCode);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return builder.indexOf("run ok") > -1;
    }

    public static void main(String[] args) {

    }
}
