当前位置:   article > 正文

判断两文件中的内容是否一致_如何查看两个so库内容是否一致

如何查看两个so库内容是否一致

1、判断两文件中的内容是否一致的shell脚本内容

  1. #!/bin/bash
  2. file1=$1
  3. file2=$2
  4. if [ -z $file1 ]; then 
  5. exit 1
  6. fi
  7. if [ -z $file2 ]; then
  8. exit 2
  9. fi
  10. type=$3
  11. if [ -z $type ];then 
  12. type=1
  13. fi
  14. sed -i '/^#/d' $file1
  15. sed -i '/^#/d' $file2
  16. sed -i '/^[ ]*$/d' $file1
  17. sed -i '/^[ ]*$/d' $file2
  18. java -cp /root/homework/CheckFile/IsSameFiles.jar com.utils.IsSameFiles.App $file1 $file2 $type

2、IsSameFiles.jar文件中的内容

①com.utils.IsSameFiles.App主类中的main()方法

  1. public static void main( String[] args ) {
  2. if(args.length < 2) {
  3. System.out.println("You need to enter at least two parameters!");
  4. return;
  5. }
  6. String file1 = args[0];
  7. String file2 = args[1];
  8. int type = 1;
  9. if(args.length >= 3) {
  10. type = Integer.parseInt(args[2]);
  11. }
  12. boolean result = false;
  13. try {
  14. // type 表示检查文件的类型,1:小文件MD5值,2:大文件MD5值,3:小文件有效内容,4:大文件有效内容
  15. switch(type) {
  16. case 1:{
  17. // 比较两个小文件MD5是否相等
  18. long startTime = System.currentTimeMillis();
  19. result = CheckLittleFilesMD5.checkFiles(file1, file2);
  20. long endTime = System.currentTimeMillis();
  21. System.out.println("CheckLittleFilesMD5.checkFiles() use time " + (endTime - startTime) + "ms");
  22. break;
  23. }
  24. case 2:{
  25. // 比较两个大文件MD5是否相等
  26. long startTime = System.currentTimeMillis();
  27. result = CheckBigFilesMD5.checkFiles(file1, file2);
  28. long endTime = System.currentTimeMillis();
  29. System.out.println("CheckBigFilesMD5.checkFile() use time " + (endTime - startTime) + "ms");
  30. break;
  31. }
  32. case 3:{
  33. // 比较两个小文件具体内容的新增与删减
  34. long startTime = System.currentTimeMillis();
  35. List<String> resultList = CheckLittleFilesContent.checkFiles(file1, file2);
  36. long endTime = System.currentTimeMillis();
  37. if(!resultList.isEmpty()) {
  38. System.out.println("+++++++++ unequal +++++++++++++");
  39. System.out.println("+++++++++ 标号'-'代表src文件中删除的内容,标号'+'代表opt文件中新增的内容 +++++++++++++");
  40. }
  41. System.out.println("CheckLittleFilesContent.checkFile() use time " + (endTime - startTime) + "ms");
  42. for(String res : resultList) {
  43. System.out.println(res);
  44. }
  45. break;
  46. }
  47. }
  48. } catch (IOException e) {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. }
  52. }

②CheckLittleFilesContent类的具体内容

  1. public class CheckLittleFilesContent {
  2. protected static char hexDigits[] =
  3. { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  4. protected static MessageDigest messageDigest = null;
  5. static {
  6. try {
  7. messageDigest = MessageDigest.getInstance("MD5");
  8. } catch (NoSuchAlgorithmException nsaex) {
  9. System.err.println(CheckBigFilesMD5.class.getName()+"初始化失败,MessageDigest不支持MD5!");
  10. nsaex.printStackTrace();
  11. }
  12. }
  13. private static final int buffSize = 1 * 1024;
  14. // key:文件中行内容的MD5,value:文件中行的内容
  15. private static Map<String, String> srcFileMap = new HashMap<String, String>();
  16. private static Map<String, String> optFileMap = new HashMap<String, String>();
  17. // List的下表代表行号,值代表文件中当前行内容的MD5
  18. private static List<String> srcFileMD5List = new ArrayList<String>();
  19. private static List<String> optFileMD5List = new ArrayList<String>();
  20. /**
  21. * 检查两个小文件有效内容是否相等
  22. * @return boolean true 相等
  23. * @throws IOException
  24. * @throws IOException
  25. */
  26. public static List<String> checkFiles(String srcFilePath, String optFilePath) throws IOException {
  27. File srcFile = new File(srcFilePath);
  28. File optFile = new File(optFilePath);
  29. if (srcFile.length() == optFile.length()) {
  30. System.out.println("+++++++++ equals +++++++++++++");
  31. return new ArrayList<String>();
  32. }
  33. // 加载需要比较的两个文件到内存中
  34. loadFiles(srcFilePath, optFilePath);
  35. // 求srcFileMD5List与optFileMD5List的差集
  36. List<String> tmplist = new ArrayList<String>();
  37. tmplist.addAll(srcFileMD5List);
  38. srcFileMD5List.removeAll(optFileMD5List);
  39. optFileMD5List.removeAll(tmplist);
  40. // // 求srcFileMD5List与optFileMD5List的交集
  41. // List<String> tmplist = new ArrayList<String>();
  42. // tmplist.addAll(srcFileMD5List);
  43. // tmplist.retainAll(optFileMD5List);
  44. //
  45. // // 求srcFileMD5List与optFileMD5List的差集
  46. // srcFileMD5List.removeAll(tmplist);
  47. // optFileMD5List.removeAll(tmplist);
  48. List<String> resultList = new ArrayList<String>();
  49. for(String srcFileMD5 : srcFileMD5List) {
  50. String srcFileContent = srcFileMap.get(srcFileMD5);
  51. resultList.add(srcFileContent);
  52. }
  53. for(String optFileMD5 : optFileMD5List) {
  54. String optFileContent = optFileMap.get(optFileMD5);
  55. resultList.add(optFileContent);
  56. }
  57. return resultList;
  58. }
  59. private static void loadFiles(String srcFilePath, String optFilePath) throws IOException {
  60. File file = new File(srcFilePath);
  61. InputStream in = new FileInputStream(file);
  62. InputStreamReader isr = new InputStreamReader(in);// InputStreamReader 是字节流通向字符流的桥梁,
  63. BufferedReader br = new BufferedReader(isr);
  64. String buff = "";
  65. long lineIndex = 0;
  66. StringBuffer sb = new StringBuffer();
  67. while((buff = br.readLine()) != null) {
  68. lineIndex++;
  69. String srcMD5 = getMD5String(buff);
  70. srcFileMap.put(srcMD5, sb.append("-").append(lineIndex).append("---").append(buff).toString());
  71. srcFileMD5List.add(srcMD5);
  72. sb.setLength(0);
  73. }
  74. file = new File(optFilePath);
  75. in = new FileInputStream(file);
  76. isr = new InputStreamReader(in);// InputStreamReader 是字节流通向字符流的桥梁,
  77. br = new BufferedReader(isr);
  78. lineIndex = 0;
  79. while((buff = br.readLine()) != null) {
  80. lineIndex++;
  81. String optMD5 = getMD5String(buff);
  82. optFileMap.put(optMD5, sb.append("+").append(lineIndex).append("---").append(buff).toString());
  83. optFileMD5List.add(optMD5);
  84. sb.setLength(0);
  85. }
  86. try {
  87. if(br != null)
  88. br.close();
  89. if(isr != null)
  90. isr.close();
  91. if(in != null) {
  92. in.close();
  93. }
  94. }catch(IOException e) {
  95. if(br != null)
  96. br.close();
  97. if(isr != null)
  98. isr.close();
  99. if(in != null) {
  100. in.close();
  101. }
  102. }finally {
  103. if(br != null)
  104. br=null;
  105. if(isr != null)
  106. isr=null;
  107. if(in != null) {
  108. in=null;
  109. }
  110. }
  111. }
  112. private static String getMD5String(String s) {
  113. return getMD5String(s.getBytes());
  114. }
  115. private static String getMD5String(byte[] bytes) {
  116. messageDigest.update(bytes);
  117. return bufferToHex(messageDigest.digest());
  118. }
  119. private static String bufferToHex(byte bytes[]) {
  120. return bufferToHex(bytes, 0, bytes.length);
  121. }
  122. private static String bufferToHex(byte bytes[], int m, int n) {
  123. StringBuffer stringbuffer = new StringBuffer(2 * n);
  124. int k = m + n;
  125. for (int l = m; l < k; l++) {
  126. appendHexPair(bytes[l], stringbuffer);
  127. }
  128. return stringbuffer.toString();
  129. }
  130. private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
  131. char c0 = hexDigits[(bt & 0xf0) >> 4];
  132. char c1 = hexDigits[bt & 0xf];
  133. stringbuffer.append(c0);
  134. stringbuffer.append(c1);
  135. }
  136. }

③CheckLittleFilesMD5类的具体内容

  1. public class CheckLittleFilesMD5 {
  2. /**
  3. * 检查两个小文件MD5是否相等
  4. * @return boolean true 相等
  5. * @throws IOException
  6. */
  7. public static boolean checkFiles(String tmpFile1, String tmpFile2) throws IOException {
  8. File file1 = new File(tmpFile1);
  9. File file2 = new File(tmpFile2);
  10. if (file1.length() != file2.length()) {
  11. System.out.println("+++++++++ unequal +++++++++++++");
  12. return false;
  13. }
  14. InputStream fileStream1 = new FileInputStream(file1);
  15. InputStream fileStream2 = new FileInputStream(file2);
  16. // InputStream 转 byte[]
  17. byte[] imageByteArray = new byte[fileStream1.available()];
  18. return isSameFiles(imageByteArray, new byte[fileStream2.available()]);
  19. }
  20. /**
  21. * 验证两个文件字节流是否相等
  22. * @return boolean true 相等
  23. * @throws IOException
  24. */
  25. private static boolean isSameFiles(byte[] fileByte1, byte[] fileByte2) {
  26. String firstFileMd5 = DigestUtils.md5Hex(fileByte1);
  27. String secondFileMd5 = DigestUtils.md5Hex(fileByte2);
  28. if (firstFileMd5.equals(secondFileMd5)) {
  29. System.out.println("---- equals ------ md5 " + firstFileMd5);
  30. return true;
  31. } else {
  32. System.out.println(firstFileMd5 + " is firstFileMd5 ++ unequal ++ secondFileMd5 = " + secondFileMd5);
  33. return false;
  34. }
  35. }
  36. }

④CheckBigFilesMD5类的具体内容

  1. public class CheckBigFilesMD5 {
  2. protected static char hexDigits[] =
  3. { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  4. protected static MessageDigest messageDigest = null;
  5. static {
  6. try {
  7. messageDigest = MessageDigest.getInstance("MD5");
  8. } catch (NoSuchAlgorithmException nsaex) {
  9. System.err.println(CheckBigFilesMD5.class.getName()+"初始化失败,MessageDigest不支持MD5!");
  10. nsaex.printStackTrace();
  11. }
  12. }
  13. /**
  14. * 检查两个大文件MD5是否相等
  15. * @return boolean true 相等
  16. * @throws IOException
  17. */
  18. public static boolean checkFiles(String srcFile, String optFile) throws IOException {
  19. File tmpSrcFile = new File(srcFile);
  20. File tmpOptFile = new File(optFile);
  21. if (tmpSrcFile.length() != tmpOptFile.length()) {
  22. System.out.println("+++++++++ unequal +++++++++++++");
  23. return false;
  24. }
  25. String srcFileMD5 = getFileMD5String(tmpSrcFile);
  26. String optFileMD5 = getFileMD5String(tmpOptFile);
  27. if(srcFileMD5.equals(optFileMD5)) {
  28. System.out.println("---- equals ------ md5 " + srcFileMD5);
  29. return true;
  30. } else {
  31. System.out.println(srcFileMD5 + " is srcFileMD5 ++ unequal ++ optFileMD5 = " + optFileMD5);
  32. return false;
  33. }
  34. }
  35. public static String getFileMD5String(File file) throws IOException {
  36. FileInputStream in = new FileInputStream(file);
  37. FileChannel ch = in.getChannel();
  38. //700000000 bytes are about 670M
  39. int maxSize=700000000;
  40. long startPosition=0L;
  41. long step=file.length()/maxSize;
  42. if(step == 0){
  43. MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
  44. messageDigest.update(byteBuffer);
  45. return bufferToHex(messageDigest.digest());
  46. }
  47. for(int i=0;i<step;i++){
  48. MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition,maxSize);
  49. messageDigest.update(byteBuffer);
  50. startPosition+=maxSize;
  51. }
  52. if(startPosition==file.length()){
  53. return bufferToHex(messageDigest.digest());
  54. }
  55. MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition,file.length()-startPosition);
  56. messageDigest.update(byteBuffer);
  57. return bufferToHex(messageDigest.digest());
  58. }
  59. public static String getMD5String(String s) {
  60. return getMD5String(s.getBytes());
  61. }
  62. public static String getMD5String(byte[] bytes) {
  63. messageDigest.update(bytes);
  64. return bufferToHex(messageDigest.digest());
  65. }
  66. private static String bufferToHex(byte bytes[]) {
  67. return bufferToHex(bytes, 0, bytes.length);
  68. }
  69. private static String bufferToHex(byte bytes[], int m, int n) {
  70. StringBuffer stringbuffer = new StringBuffer(2 * n);
  71. int k = m + n;
  72. for (int l = m; l < k; l++) {
  73. appendHexPair(bytes[l], stringbuffer);
  74. }
  75. return stringbuffer.toString();
  76. }
  77. private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
  78. char c0 = hexDigits[(bt & 0xf0) >> 4];
  79. char c1 = hexDigits[bt & 0xf];
  80. stringbuffer.append(c0);
  81. stringbuffer.append(c1);
  82. }
  83. public static boolean checkPassword(String password, String md5PwdStr) {
  84. String s = getMD5String(password);
  85. return s.equals(md5PwdStr);
  86. }
  87. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/871643
推荐阅读
相关标签
  

闽ICP备14008679号